1 /*******************************************************************************
2 * IBM Virtual SCSI Target Driver
3 * Copyright (C) 2003-2005 Dave Boutcher (boutcher@us.ibm.com) IBM Corp.
4 * Santiago Leon (santil@us.ibm.com) IBM Corp.
5 * Linda Xie (lxie@us.ibm.com) IBM Corp.
7 * Copyright (C) 2005-2011 FUJITA Tomonori <tomof@acm.org>
8 * Copyright (C) 2010 Nicholas A. Bellinger <nab@kernel.org>
10 * Authors: Bryant G. Ly <bryantly@linux.vnet.ibm.com>
11 * Authors: Michael Cyr <mikecyr@linux.vnet.ibm.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 ****************************************************************************/
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/types.h>
31 #include <linux/list.h>
32 #include <linux/string.h>
33 #include <linux/delay.h>
35 #include <target/target_core_base.h>
36 #include <target/target_core_fabric.h>
38 #include <asm/hvcall.h>
41 #include <scsi/viosrp.h>
43 #include "ibmvscsi_tgt.h"
45 #define IBMVSCSIS_VERSION "v0.2"
47 #define INITIAL_SRP_LIMIT 800
48 #define DEFAULT_MAX_SECTORS 256
50 static uint max_vdma_size = MAX_H_COPY_RDMA;
52 static char system_id[SYS_ID_NAME_LEN] = "";
53 static char partition_name[PARTITION_NAMELEN] = "UNKNOWN";
54 static uint partition_number = -1;
56 /* Adapter list and lock to control it */
57 static DEFINE_SPINLOCK(ibmvscsis_dev_lock);
58 static LIST_HEAD(ibmvscsis_dev_list);
60 static long ibmvscsis_parse_command(struct scsi_info *vscsi,
61 struct viosrp_crq *crq);
63 static void ibmvscsis_adapter_idle(struct scsi_info *vscsi);
65 static void ibmvscsis_determine_resid(struct se_cmd *se_cmd,
68 u32 residual_count = se_cmd->residual_count;
73 if (se_cmd->se_cmd_flags & SCF_UNDERFLOW_BIT) {
74 if (se_cmd->data_direction == DMA_TO_DEVICE) {
75 /* residual data from an underflow write */
76 rsp->flags = SRP_RSP_FLAG_DOUNDER;
77 rsp->data_out_res_cnt = cpu_to_be32(residual_count);
78 } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
79 /* residual data from an underflow read */
80 rsp->flags = SRP_RSP_FLAG_DIUNDER;
81 rsp->data_in_res_cnt = cpu_to_be32(residual_count);
83 } else if (se_cmd->se_cmd_flags & SCF_OVERFLOW_BIT) {
84 if (se_cmd->data_direction == DMA_TO_DEVICE) {
85 /* residual data from an overflow write */
86 rsp->flags = SRP_RSP_FLAG_DOOVER;
87 rsp->data_out_res_cnt = cpu_to_be32(residual_count);
88 } else if (se_cmd->data_direction == DMA_FROM_DEVICE) {
89 /* residual data from an overflow read */
90 rsp->flags = SRP_RSP_FLAG_DIOVER;
91 rsp->data_in_res_cnt = cpu_to_be32(residual_count);
97 * connection_broken() - Determine if the connection to the client is good
98 * @vscsi: Pointer to our adapter structure
100 * This function attempts to send a ping MAD to the client. If the call to
101 * queue the request returns H_CLOSED then the connection has been broken
102 * and the function returns TRUE.
104 * EXECUTION ENVIRONMENT:
105 * Interrupt or Process environment
107 static bool connection_broken(struct scsi_info *vscsi)
109 struct viosrp_crq *crq;
110 u64 buffer[2] = { 0, 0 };
114 /* create a PING crq */
115 crq = (struct viosrp_crq *)&buffer;
116 crq->valid = VALID_CMD_RESP_EL;
117 crq->format = MESSAGE_IN_CRQ;
120 h_return_code = h_send_crq(vscsi->dds.unit_id,
121 cpu_to_be64(buffer[MSG_HI]),
122 cpu_to_be64(buffer[MSG_LOW]));
124 pr_debug("connection_broken: rc %ld\n", h_return_code);
126 if (h_return_code == H_CLOSED)
133 * ibmvscsis_unregister_command_q() - Helper Function-Unregister Command Queue
134 * @vscsi: Pointer to our adapter structure
136 * This function calls h_free_q then frees the interrupt bit etc.
137 * It must release the lock before doing so because of the time it can take
138 * for h_free_crq in PHYP
139 * NOTE: the caller must make sure that state and or flags will prevent
140 * interrupt handler from scheduling work.
141 * NOTE: anyone calling this function may need to set the CRQ_CLOSED flag
142 * we can't do it here, because we don't have the lock
144 * EXECUTION ENVIRONMENT:
147 static long ibmvscsis_unregister_command_q(struct scsi_info *vscsi)
150 long rc = ADAPT_SUCCESS;
154 qrc = h_free_crq(vscsi->dds.unit_id);
161 dev_err(&vscsi->dev, "unregister_command_q: error from h_free_crq %ld\n",
167 case H_LONG_BUSY_ORDER_1_MSEC:
168 /* msleep not good for small values */
169 usleep_range(1000, 2000);
172 case H_LONG_BUSY_ORDER_10_MSEC:
173 usleep_range(10000, 20000);
176 case H_LONG_BUSY_ORDER_100_MSEC:
180 case H_LONG_BUSY_ORDER_1_SEC:
184 case H_LONG_BUSY_ORDER_10_SEC:
188 case H_LONG_BUSY_ORDER_100_SEC:
193 dev_err(&vscsi->dev, "unregister_command_q: unknown error %ld from h_free_crq\n",
200 * dont wait more then 300 seconds
201 * ticks are in milliseconds more or less
203 if (ticks > 300000 && qrc != H_SUCCESS) {
205 dev_err(&vscsi->dev, "Excessive wait for h_free_crq\n");
207 } while (qrc != H_SUCCESS && rc == ADAPT_SUCCESS);
209 pr_debug("Freeing CRQ: phyp rc %ld, rc %ld\n", qrc, rc);
215 * ibmvscsis_delete_client_info() - Helper function to Delete Client Info
216 * @vscsi: Pointer to our adapter structure
217 * @client_closed: True if client closed its queue
219 * Deletes information specific to the client when the client goes away
221 * EXECUTION ENVIRONMENT:
222 * Interrupt or Process
224 static void ibmvscsis_delete_client_info(struct scsi_info *vscsi,
227 vscsi->client_cap = 0;
230 * Some things we don't want to clear if we're closing the queue,
231 * because some clients don't resend the host handshake when they
232 * get a transport event.
235 vscsi->client_data.os_type = 0;
239 * ibmvscsis_free_command_q() - Free Command Queue
240 * @vscsi: Pointer to our adapter structure
242 * This function calls unregister_command_q, then clears interrupts and
243 * any pending interrupt acknowledgments associated with the command q.
244 * It also clears memory if there is no error.
246 * PHYP did not meet the PAPR architecture so that we must give up the
247 * lock. This causes a timing hole regarding state change. To close the
248 * hole this routine does accounting on any change that occurred during
249 * the time the lock is not held.
250 * NOTE: must give up and then acquire the interrupt lock, the caller must
251 * make sure that state and or flags will prevent interrupt handler from
254 * EXECUTION ENVIRONMENT:
255 * Process level, interrupt lock is held
257 static long ibmvscsis_free_command_q(struct scsi_info *vscsi)
260 u32 flags_under_lock;
261 u16 state_under_lock;
262 long rc = ADAPT_SUCCESS;
264 if (!(vscsi->flags & CRQ_CLOSED)) {
265 vio_disable_interrupts(vscsi->dma_dev);
267 state_under_lock = vscsi->new_state;
268 flags_under_lock = vscsi->flags;
269 vscsi->phyp_acr_state = 0;
270 vscsi->phyp_acr_flags = 0;
272 spin_unlock_bh(&vscsi->intr_lock);
273 rc = ibmvscsis_unregister_command_q(vscsi);
274 spin_lock_bh(&vscsi->intr_lock);
276 if (state_under_lock != vscsi->new_state)
277 vscsi->phyp_acr_state = vscsi->new_state;
279 vscsi->phyp_acr_flags = ((~flags_under_lock) & vscsi->flags);
281 if (rc == ADAPT_SUCCESS) {
282 bytes = vscsi->cmd_q.size * PAGE_SIZE;
283 memset(vscsi->cmd_q.base_addr, 0, bytes);
284 vscsi->cmd_q.index = 0;
285 vscsi->flags |= CRQ_CLOSED;
287 ibmvscsis_delete_client_info(vscsi, false);
290 pr_debug("free_command_q: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
291 vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
292 vscsi->phyp_acr_state);
298 * ibmvscsis_cmd_q_dequeue() - Get valid Command element
299 * @mask: Mask to use in case index wraps
300 * @current_index: Current index into command queue
301 * @base_addr: Pointer to start of command queue
303 * Returns a pointer to a valid command element or NULL, if the command
306 * EXECUTION ENVIRONMENT:
307 * Interrupt environment, interrupt lock held
309 static struct viosrp_crq *ibmvscsis_cmd_q_dequeue(uint mask,
311 struct viosrp_crq *base_addr)
313 struct viosrp_crq *ptr;
315 ptr = base_addr + *current_index;
318 *current_index = (*current_index + 1) & mask;
328 * ibmvscsis_send_init_message() - send initialize message to the client
329 * @vscsi: Pointer to our adapter structure
330 * @format: Which Init Message format to send
332 * EXECUTION ENVIRONMENT:
333 * Interrupt environment interrupt lock held
335 static long ibmvscsis_send_init_message(struct scsi_info *vscsi, u8 format)
337 struct viosrp_crq *crq;
338 u64 buffer[2] = { 0, 0 };
341 crq = (struct viosrp_crq *)&buffer;
342 crq->valid = VALID_INIT_MSG;
343 crq->format = format;
344 rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
345 cpu_to_be64(buffer[MSG_LOW]));
351 * ibmvscsis_check_init_msg() - Check init message valid
352 * @vscsi: Pointer to our adapter structure
353 * @format: Pointer to return format of Init Message, if any.
354 * Set to UNUSED_FORMAT if no Init Message in queue.
356 * Checks if an initialize message was queued by the initiatior
357 * after the queue was created and before the interrupt was enabled.
359 * EXECUTION ENVIRONMENT:
360 * Process level only, interrupt lock held
362 static long ibmvscsis_check_init_msg(struct scsi_info *vscsi, uint *format)
364 struct viosrp_crq *crq;
365 long rc = ADAPT_SUCCESS;
367 crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask, &vscsi->cmd_q.index,
368 vscsi->cmd_q.base_addr);
370 *format = (uint)UNUSED_FORMAT;
371 } else if (crq->valid == VALID_INIT_MSG && crq->format == INIT_MSG) {
372 *format = (uint)INIT_MSG;
373 crq->valid = INVALIDATE_CMD_RESP_EL;
377 * the caller has ensured no initialize message was
378 * sent after the queue was
379 * created so there should be no other message on the queue.
381 crq = ibmvscsis_cmd_q_dequeue(vscsi->cmd_q.mask,
383 vscsi->cmd_q.base_addr);
385 *format = (uint)(crq->format);
387 crq->valid = INVALIDATE_CMD_RESP_EL;
391 *format = (uint)(crq->format);
393 crq->valid = INVALIDATE_CMD_RESP_EL;
401 * ibmvscsis_disconnect() - Helper function to disconnect
402 * @work: Pointer to work_struct, gives access to our adapter structure
404 * An error has occurred or the driver received a Transport event,
405 * and the driver is requesting that the command queue be de-registered
406 * in a safe manner. If there is no outstanding I/O then we can stop the
407 * queue. If we are restarting the queue it will be reflected in the
408 * the state of the adapter.
410 * EXECUTION ENVIRONMENT:
411 * Process environment
413 static void ibmvscsis_disconnect(struct work_struct *work)
415 struct scsi_info *vscsi = container_of(work, struct scsi_info,
418 bool wait_idle = false;
420 spin_lock_bh(&vscsi->intr_lock);
421 new_state = vscsi->new_state;
422 vscsi->new_state = 0;
424 pr_debug("disconnect: flags 0x%x, state 0x%hx\n", vscsi->flags,
428 * check which state we are in and see if we
429 * should transitition to the new state
431 switch (vscsi->state) {
432 /* Should never be called while in this state. */
435 * Can never transition from this state;
436 * igonore errors and logout.
441 /* can transition from this state to UNCONFIGURING */
443 if (new_state == UNCONFIGURING)
444 vscsi->state = new_state;
448 * Can transition from this state to to unconfiguring
451 case ERR_DISCONNECT_RECONNECT:
455 vscsi->state = new_state;
465 /* can transition from this state to UNCONFIGURING */
466 case ERR_DISCONNECTED:
467 if (new_state == UNCONFIGURING)
468 vscsi->state = new_state;
474 vscsi->state = new_state;
475 vscsi->flags |= RESPONSE_Q_DOWN;
476 vscsi->flags &= ~(SCHEDULE_DISCONNECT |
477 DISCONNECT_SCHEDULED);
479 if (vscsi->flags & CFG_SLEEPING) {
480 vscsi->flags &= ~CFG_SLEEPING;
481 complete(&vscsi->unconfig);
485 /* should never happen */
487 case ERR_DISCONNECT_RECONNECT:
489 dev_err(&vscsi->dev, "disconnect: invalid state %d for WAIT_IDLE\n",
498 vscsi->flags |= RESPONSE_Q_DOWN;
499 vscsi->state = new_state;
500 vscsi->flags &= ~(SCHEDULE_DISCONNECT |
501 DISCONNECT_SCHEDULED);
502 ibmvscsis_free_command_q(vscsi);
505 case ERR_DISCONNECT_RECONNECT:
506 vscsi->state = new_state;
512 * Initiator has not done a successful srp login
513 * or has done a successful srp logout ( adapter was not
514 * busy). In the first case there can be responses queued
515 * waiting for space on the initiators response queue (MAD)
516 * The second case the adapter is idle. Assume the worse case,
517 * i.e. the second case.
519 case WAIT_CONNECTION:
523 vscsi->state = new_state;
526 /* can transition from this state to UNCONFIGURING */
528 if (new_state == UNCONFIGURING)
529 vscsi->state = new_state;
536 pr_debug("disconnect start wait, active %d, sched %d\n",
537 (int)list_empty(&vscsi->active_q),
538 (int)list_empty(&vscsi->schedule_q));
539 if (!list_empty(&vscsi->active_q) ||
540 !list_empty(&vscsi->schedule_q)) {
541 vscsi->flags |= WAIT_FOR_IDLE;
542 pr_debug("disconnect flags 0x%x\n", vscsi->flags);
544 * This routine is can not be called with the interrupt
547 spin_unlock_bh(&vscsi->intr_lock);
548 wait_for_completion(&vscsi->wait_idle);
549 spin_lock_bh(&vscsi->intr_lock);
551 pr_debug("disconnect stop wait\n");
553 ibmvscsis_adapter_idle(vscsi);
556 spin_unlock_bh(&vscsi->intr_lock);
560 * ibmvscsis_post_disconnect() - Schedule the disconnect
561 * @vscsi: Pointer to our adapter structure
562 * @new_state: State to move to after disconnecting
563 * @flag_bits: Flags to turn on in adapter structure
565 * If it's already been scheduled, then see if we need to "upgrade"
566 * the new state (if the one passed in is more "severe" than the
570 * interrupt lock is held
572 static void ibmvscsis_post_disconnect(struct scsi_info *vscsi, uint new_state,
577 /* check the validity of the new state */
581 case ERR_DISCONNECT_RECONNECT:
586 dev_err(&vscsi->dev, "post_disconnect: Invalid new state %d\n",
591 vscsi->flags |= flag_bits;
593 pr_debug("post_disconnect: new_state 0x%x, flag_bits 0x%x, vscsi->flags 0x%x, state %hx\n",
594 new_state, flag_bits, vscsi->flags, vscsi->state);
596 if (!(vscsi->flags & (DISCONNECT_SCHEDULED | SCHEDULE_DISCONNECT))) {
597 vscsi->flags |= SCHEDULE_DISCONNECT;
598 vscsi->new_state = new_state;
600 INIT_WORK(&vscsi->proc_work, ibmvscsis_disconnect);
601 (void)queue_work(vscsi->work_q, &vscsi->proc_work);
603 if (vscsi->new_state)
604 state = vscsi->new_state;
606 state = vscsi->state;
613 case ERR_DISCONNECTED:
616 if (new_state == UNCONFIGURING)
617 vscsi->new_state = new_state;
620 case ERR_DISCONNECT_RECONNECT:
624 vscsi->new_state = new_state;
633 case WAIT_CONNECTION:
636 vscsi->new_state = new_state;
644 pr_debug("Leaving post_disconnect: flags 0x%x, new_state 0x%x\n",
645 vscsi->flags, vscsi->new_state);
649 * ibmvscsis_handle_init_compl_msg() - Respond to an Init Complete Message
650 * @vscsi: Pointer to our adapter structure
652 * Must be called with interrupt lock held.
654 static long ibmvscsis_handle_init_compl_msg(struct scsi_info *vscsi)
656 long rc = ADAPT_SUCCESS;
658 switch (vscsi->state) {
661 case ERR_DISCONNECT_RECONNECT:
662 case ERR_DISCONNECTED:
668 case WAIT_CONNECTION:
669 vscsi->state = CONNECTED;
678 dev_err(&vscsi->dev, "init_msg: invalid state %d to get init compl msg\n",
680 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
688 * ibmvscsis_handle_init_msg() - Respond to an Init Message
689 * @vscsi: Pointer to our adapter structure
691 * Must be called with interrupt lock held.
693 static long ibmvscsis_handle_init_msg(struct scsi_info *vscsi)
695 long rc = ADAPT_SUCCESS;
697 switch (vscsi->state) {
698 case WAIT_CONNECTION:
699 rc = ibmvscsis_send_init_message(vscsi, INIT_COMPLETE_MSG);
702 vscsi->state = CONNECTED;
706 dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
708 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
712 dev_err(&vscsi->dev, "init_msg: failed to send, rc %ld\n",
715 ibmvscsis_post_disconnect(vscsi,
716 ERR_DISCONNECT_RECONNECT, 0);
720 pr_warn("init_msg: failed to send, rc %ld\n", rc);
739 case ERR_DISCONNECT_RECONNECT:
740 case ERR_DISCONNECTED:
743 dev_err(&vscsi->dev, "init_msg: invalid state %d to get init msg\n",
745 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
753 * ibmvscsis_init_msg() - Respond to an init message
754 * @vscsi: Pointer to our adapter structure
755 * @crq: Pointer to CRQ element containing the Init Message
757 * EXECUTION ENVIRONMENT:
758 * Interrupt, interrupt lock held
760 static long ibmvscsis_init_msg(struct scsi_info *vscsi, struct viosrp_crq *crq)
762 long rc = ADAPT_SUCCESS;
764 pr_debug("init_msg: state 0x%hx\n", vscsi->state);
766 rc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
767 (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
769 if (rc == H_SUCCESS) {
770 vscsi->client_data.partition_number =
771 be64_to_cpu(*(u64 *)vscsi->map_buf);
772 pr_debug("init_msg, part num %d\n",
773 vscsi->client_data.partition_number);
775 pr_debug("init_msg h_vioctl rc %ld\n", rc);
779 if (crq->format == INIT_MSG) {
780 rc = ibmvscsis_handle_init_msg(vscsi);
781 } else if (crq->format == INIT_COMPLETE_MSG) {
782 rc = ibmvscsis_handle_init_compl_msg(vscsi);
785 dev_err(&vscsi->dev, "init_msg: invalid format %d\n",
787 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
794 * ibmvscsis_establish_new_q() - Establish new CRQ queue
795 * @vscsi: Pointer to our adapter structure
797 * Must be called with interrupt lock held.
799 static long ibmvscsis_establish_new_q(struct scsi_info *vscsi)
801 long rc = ADAPT_SUCCESS;
804 vscsi->flags &= PRESERVE_FLAG_FIELDS;
805 vscsi->rsp_q_timer.timer_pops = 0;
809 rc = vio_enable_interrupts(vscsi->dma_dev);
811 pr_warn("establish_new_q: failed to enable interrupts, rc %ld\n",
816 rc = ibmvscsis_check_init_msg(vscsi, &format);
818 dev_err(&vscsi->dev, "establish_new_q: check_init_msg failed, rc %ld\n",
823 if (format == UNUSED_FORMAT) {
824 rc = ibmvscsis_send_init_message(vscsi, INIT_MSG);
837 vscsi->state = UNDEFINED;
841 } else if (format == INIT_MSG) {
842 rc = ibmvscsis_handle_init_msg(vscsi);
849 * ibmvscsis_reset_queue() - Reset CRQ Queue
850 * @vscsi: Pointer to our adapter structure
852 * This function calls h_free_q and then calls h_reg_q and does all
853 * of the bookkeeping to get us back to where we can communicate.
855 * Actually, we don't always call h_free_crq. A problem was discovered
856 * where one partition would close and reopen his queue, which would
857 * cause his partner to get a transport event, which would cause him to
858 * close and reopen his queue, which would cause the original partition
859 * to get a transport event, etc., etc. To prevent this, we don't
860 * actually close our queue if the client initiated the reset, (i.e.
861 * either we got a transport event or we have detected that the client's
864 * EXECUTION ENVIRONMENT:
865 * Process environment, called with interrupt lock held
867 static void ibmvscsis_reset_queue(struct scsi_info *vscsi)
870 long rc = ADAPT_SUCCESS;
872 pr_debug("reset_queue: flags 0x%x\n", vscsi->flags);
874 /* don't reset, the client did it for us */
875 if (vscsi->flags & (CLIENT_FAILED | TRANS_EVENT)) {
876 vscsi->flags &= PRESERVE_FLAG_FIELDS;
877 vscsi->rsp_q_timer.timer_pops = 0;
880 vscsi->state = WAIT_CONNECTION;
881 vio_enable_interrupts(vscsi->dma_dev);
883 rc = ibmvscsis_free_command_q(vscsi);
884 if (rc == ADAPT_SUCCESS) {
885 vscsi->state = WAIT_CONNECTION;
887 bytes = vscsi->cmd_q.size * PAGE_SIZE;
888 rc = h_reg_crq(vscsi->dds.unit_id,
889 vscsi->cmd_q.crq_token, bytes);
890 if (rc == H_CLOSED || rc == H_SUCCESS) {
891 rc = ibmvscsis_establish_new_q(vscsi);
894 if (rc != ADAPT_SUCCESS) {
895 pr_debug("reset_queue: reg_crq rc %ld\n", rc);
897 vscsi->state = ERR_DISCONNECTED;
898 vscsi->flags |= RESPONSE_Q_DOWN;
899 ibmvscsis_free_command_q(vscsi);
902 vscsi->state = ERR_DISCONNECTED;
903 vscsi->flags |= RESPONSE_Q_DOWN;
909 * ibmvscsis_free_cmd_resources() - Free command resources
910 * @vscsi: Pointer to our adapter structure
911 * @cmd: Command which is not longer in use
913 * Must be called with interrupt lock held.
915 static void ibmvscsis_free_cmd_resources(struct scsi_info *vscsi,
916 struct ibmvscsis_cmd *cmd)
918 struct iu_entry *iue = cmd->iue;
921 case TASK_MANAGEMENT:
924 * When the queue goes down this value is cleared, so it
925 * cannot be cleared in this general purpose function.
931 vscsi->flags &= ~PROCESSING_MAD;
936 dev_err(&vscsi->dev, "free_cmd_resources unknown type %d\n",
942 list_add_tail(&cmd->list, &vscsi->free_cmd);
945 if (list_empty(&vscsi->active_q) && list_empty(&vscsi->schedule_q) &&
946 list_empty(&vscsi->waiting_rsp) && (vscsi->flags & WAIT_FOR_IDLE)) {
947 vscsi->flags &= ~WAIT_FOR_IDLE;
948 complete(&vscsi->wait_idle);
953 * ibmvscsis_trans_event() - Handle a Transport Event
954 * @vscsi: Pointer to our adapter structure
955 * @crq: Pointer to CRQ entry containing the Transport Event
957 * Do the logic to close the I_T nexus. This function may not
958 * behave to specification.
960 * EXECUTION ENVIRONMENT:
961 * Interrupt, interrupt lock held
963 static long ibmvscsis_trans_event(struct scsi_info *vscsi,
964 struct viosrp_crq *crq)
966 long rc = ADAPT_SUCCESS;
968 pr_debug("trans_event: format %d, flags 0x%x, state 0x%hx\n",
969 (int)crq->format, vscsi->flags, vscsi->state);
971 switch (crq->format) {
974 case PARTNER_DEREGISTER:
975 ibmvscsis_delete_client_info(vscsi, true);
980 dev_err(&vscsi->dev, "trans_event: invalid format %d\n",
982 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT,
987 if (rc == ADAPT_SUCCESS) {
988 switch (vscsi->state) {
990 case ERR_DISCONNECTED:
995 vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
1001 case WAIT_CONNECTION:
1005 ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
1010 case SRP_PROCESSING:
1011 if ((vscsi->debit > 0) ||
1012 !list_empty(&vscsi->schedule_q) ||
1013 !list_empty(&vscsi->waiting_rsp) ||
1014 !list_empty(&vscsi->active_q)) {
1015 pr_debug("debit %d, sched %d, wait %d, active %d\n",
1017 (int)list_empty(&vscsi->schedule_q),
1018 (int)list_empty(&vscsi->waiting_rsp),
1019 (int)list_empty(&vscsi->active_q));
1020 pr_warn("connection lost with outstanding work\n");
1022 pr_debug("trans_event: SRP Processing, but no outstanding work\n");
1025 ibmvscsis_post_disconnect(vscsi, WAIT_IDLE,
1030 case ERR_DISCONNECT:
1031 case ERR_DISCONNECT_RECONNECT:
1033 vscsi->flags |= (RESPONSE_Q_DOWN | TRANS_EVENT);
1038 rc = vscsi->flags & SCHEDULE_DISCONNECT;
1040 pr_debug("Leaving trans_event: flags 0x%x, state 0x%hx, rc %ld\n",
1041 vscsi->flags, vscsi->state, rc);
1047 * ibmvscsis_poll_cmd_q() - Poll Command Queue
1048 * @vscsi: Pointer to our adapter structure
1050 * Called to handle command elements that may have arrived while
1051 * interrupts were disabled.
1053 * EXECUTION ENVIRONMENT:
1054 * intr_lock must be held
1056 static void ibmvscsis_poll_cmd_q(struct scsi_info *vscsi)
1058 struct viosrp_crq *crq;
1063 pr_debug("poll_cmd_q: flags 0x%x, state 0x%hx, q index %ud\n",
1064 vscsi->flags, vscsi->state, vscsi->cmd_q.index);
1066 rc = vscsi->flags & SCHEDULE_DISCONNECT;
1067 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
1073 vscsi->cmd_q.index =
1074 (vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
1077 rc = ibmvscsis_parse_command(vscsi, crq);
1079 if ((uint)crq->valid == VALID_TRANS_EVENT) {
1081 * must service the transport layer events even
1082 * in an error state, dont break out until all
1083 * the consecutive transport events have been
1086 rc = ibmvscsis_trans_event(vscsi, crq);
1087 } else if (vscsi->flags & TRANS_EVENT) {
1089 * if a tranport event has occurred leave
1090 * everything but transport events on the queue
1092 pr_debug("poll_cmd_q, ignoring\n");
1095 * need to decrement the queue index so we can
1096 * look at the elment again
1098 if (vscsi->cmd_q.index)
1099 vscsi->cmd_q.index -= 1;
1102 * index is at 0 it just wrapped.
1103 * have it index last element in q
1105 vscsi->cmd_q.index = vscsi->cmd_q.mask;
1110 crq->valid = INVALIDATE_CMD_RESP_EL;
1112 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
1119 vio_enable_interrupts(vscsi->dma_dev);
1121 pr_debug("poll_cmd_q, reenabling interrupts\n");
1129 pr_debug("Leaving poll_cmd_q: rc %ld\n", rc);
1133 * ibmvscsis_free_cmd_qs() - Free elements in queue
1134 * @vscsi: Pointer to our adapter structure
1136 * Free all of the elements on all queues that are waiting for
1140 * Called with interrupt lock held
1142 static void ibmvscsis_free_cmd_qs(struct scsi_info *vscsi)
1144 struct ibmvscsis_cmd *cmd, *nxt;
1146 pr_debug("free_cmd_qs: waiting_rsp empty %d, timer starter %d\n",
1147 (int)list_empty(&vscsi->waiting_rsp),
1148 vscsi->rsp_q_timer.started);
1150 list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
1151 list_del(&cmd->list);
1152 ibmvscsis_free_cmd_resources(vscsi, cmd);
1157 * ibmvscsis_get_free_cmd() - Get free command from list
1158 * @vscsi: Pointer to our adapter structure
1160 * Must be called with interrupt lock held.
1162 static struct ibmvscsis_cmd *ibmvscsis_get_free_cmd(struct scsi_info *vscsi)
1164 struct ibmvscsis_cmd *cmd = NULL;
1165 struct iu_entry *iue;
1167 iue = srp_iu_get(&vscsi->target);
1169 cmd = list_first_entry_or_null(&vscsi->free_cmd,
1170 struct ibmvscsis_cmd, list);
1172 list_del(&cmd->list);
1174 cmd->type = UNSET_TYPE;
1175 memset(&cmd->se_cmd, 0, sizeof(cmd->se_cmd));
1185 * ibmvscsis_adapter_idle() - Helper function to handle idle adapter
1186 * @vscsi: Pointer to our adapter structure
1188 * This function is called when the adapter is idle when the driver
1189 * is attempting to clear an error condition.
1190 * The adapter is considered busy if any of its cmd queues
1191 * are non-empty. This function can be invoked
1192 * from the off level disconnect function.
1194 * EXECUTION ENVIRONMENT:
1195 * Process environment called with interrupt lock held
1197 static void ibmvscsis_adapter_idle(struct scsi_info *vscsi)
1199 int free_qs = false;
1201 pr_debug("adapter_idle: flags 0x%x, state 0x%hx\n", vscsi->flags,
1204 /* Only need to free qs if we're disconnecting from client */
1205 if (vscsi->state != WAIT_CONNECTION || vscsi->flags & TRANS_EVENT)
1208 switch (vscsi->state) {
1210 ibmvscsis_free_command_q(vscsi);
1213 if (vscsi->flags & CFG_SLEEPING) {
1214 vscsi->flags &= ~CFG_SLEEPING;
1215 complete(&vscsi->unconfig);
1218 case ERR_DISCONNECT_RECONNECT:
1219 ibmvscsis_reset_queue(vscsi);
1220 pr_debug("adapter_idle, disc_rec: flags 0x%x\n", vscsi->flags);
1223 case ERR_DISCONNECT:
1224 ibmvscsis_free_command_q(vscsi);
1225 vscsi->flags &= ~(SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED);
1226 vscsi->flags |= RESPONSE_Q_DOWN;
1227 if (vscsi->tport.enabled)
1228 vscsi->state = ERR_DISCONNECTED;
1230 vscsi->state = WAIT_ENABLED;
1231 pr_debug("adapter_idle, disc: flags 0x%x, state 0x%hx\n",
1232 vscsi->flags, vscsi->state);
1236 vscsi->rsp_q_timer.timer_pops = 0;
1239 if (vscsi->flags & TRANS_EVENT) {
1240 vscsi->state = WAIT_CONNECTION;
1241 vscsi->flags &= PRESERVE_FLAG_FIELDS;
1243 vscsi->state = CONNECTED;
1244 vscsi->flags &= ~DISCONNECT_SCHEDULED;
1247 pr_debug("adapter_idle, wait: flags 0x%x, state 0x%hx\n",
1248 vscsi->flags, vscsi->state);
1249 ibmvscsis_poll_cmd_q(vscsi);
1252 case ERR_DISCONNECTED:
1253 vscsi->flags &= ~DISCONNECT_SCHEDULED;
1254 pr_debug("adapter_idle, disconnected: flags 0x%x, state 0x%hx\n",
1255 vscsi->flags, vscsi->state);
1259 dev_err(&vscsi->dev, "adapter_idle: in invalid state %d\n",
1265 ibmvscsis_free_cmd_qs(vscsi);
1268 * There is a timing window where we could lose a disconnect request.
1269 * The known path to this window occurs during the DISCONNECT_RECONNECT
1270 * case above: reset_queue calls free_command_q, which will release the
1271 * interrupt lock. During that time, a new post_disconnect call can be
1272 * made with a "more severe" state (DISCONNECT or UNCONFIGURING).
1273 * Because the DISCONNECT_SCHEDULED flag is already set, post_disconnect
1274 * will only set the new_state. Now free_command_q reacquires the intr
1275 * lock and clears the DISCONNECT_SCHEDULED flag (using PRESERVE_FLAG_
1276 * FIELDS), and the disconnect is lost. This is particularly bad when
1277 * the new disconnect was for UNCONFIGURING, since the unconfigure hangs
1279 * Fix is that free command queue sets acr state and acr flags if there
1280 * is a change under the lock
1281 * note free command queue writes to this state it clears it
1282 * before releasing the lock, different drivers call the free command
1283 * queue different times so dont initialize above
1285 if (vscsi->phyp_acr_state != 0) {
1287 * set any bits in flags that may have been cleared by
1288 * a call to free command queue in switch statement
1291 vscsi->flags |= vscsi->phyp_acr_flags;
1292 ibmvscsis_post_disconnect(vscsi, vscsi->phyp_acr_state, 0);
1293 vscsi->phyp_acr_state = 0;
1294 vscsi->phyp_acr_flags = 0;
1296 pr_debug("adapter_idle: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
1297 vscsi->flags, vscsi->state, vscsi->phyp_acr_flags,
1298 vscsi->phyp_acr_state);
1301 pr_debug("Leaving adapter_idle: flags 0x%x, state 0x%hx, new_state 0x%x\n",
1302 vscsi->flags, vscsi->state, vscsi->new_state);
1306 * ibmvscsis_copy_crq_packet() - Copy CRQ Packet
1307 * @vscsi: Pointer to our adapter structure
1308 * @cmd: Pointer to command element to use to process the request
1309 * @crq: Pointer to CRQ entry containing the request
1311 * Copy the srp information unit from the hosted
1312 * partition using remote dma
1314 * EXECUTION ENVIRONMENT:
1315 * Interrupt, interrupt lock held
1317 static long ibmvscsis_copy_crq_packet(struct scsi_info *vscsi,
1318 struct ibmvscsis_cmd *cmd,
1319 struct viosrp_crq *crq)
1321 struct iu_entry *iue = cmd->iue;
1325 len = be16_to_cpu(crq->IU_length);
1326 if ((len > SRP_MAX_IU_LEN) || (len == 0)) {
1327 dev_err(&vscsi->dev, "copy_crq: Invalid len %d passed", len);
1328 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1329 return SRP_VIOLATION;
1332 rc = h_copy_rdma(len, vscsi->dds.window[REMOTE].liobn,
1333 be64_to_cpu(crq->IU_data_ptr),
1334 vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma);
1338 cmd->init_time = mftb();
1339 iue->remote_token = crq->IU_data_ptr;
1341 pr_debug("copy_crq: ioba 0x%llx, init_time 0x%llx\n",
1342 be64_to_cpu(crq->IU_data_ptr), cmd->init_time);
1345 if (connection_broken(vscsi))
1346 ibmvscsis_post_disconnect(vscsi,
1347 ERR_DISCONNECT_RECONNECT,
1351 ibmvscsis_post_disconnect(vscsi,
1352 ERR_DISCONNECT_RECONNECT, 0);
1354 dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
1360 dev_err(&vscsi->dev, "copy_crq: h_copy_rdma failed, rc %ld\n",
1362 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1370 * ibmvscsis_adapter_info - Service an Adapter Info MAnagement Data gram
1371 * @vscsi: Pointer to our adapter structure
1372 * @iue: Information Unit containing the Adapter Info MAD request
1374 * EXECUTION ENVIRONMENT:
1375 * Interrupt adapter lock is held
1377 static long ibmvscsis_adapter_info(struct scsi_info *vscsi,
1378 struct iu_entry *iue)
1380 struct viosrp_adapter_info *mad = &vio_iu(iue)->mad.adapter_info;
1381 struct mad_adapter_info_data *info;
1386 mad->common.status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
1388 if (be16_to_cpu(mad->common.length) > sizeof(*info)) {
1389 mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1393 info = dma_alloc_coherent(&vscsi->dma_dev->dev, sizeof(*info), &token,
1396 dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
1398 mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1402 /* Get remote info */
1403 rc = h_copy_rdma(be16_to_cpu(mad->common.length),
1404 vscsi->dds.window[REMOTE].liobn,
1405 be64_to_cpu(mad->buffer),
1406 vscsi->dds.window[LOCAL].liobn, token);
1408 if (rc != H_SUCCESS) {
1409 if (rc == H_PERMISSION) {
1410 if (connection_broken(vscsi))
1411 flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
1413 pr_warn("adapter_info: h_copy_rdma from client failed, rc %ld\n",
1415 pr_debug("adapter_info: ioba 0x%llx, flags 0x%x, flag_bits 0x%x\n",
1416 be64_to_cpu(mad->buffer), vscsi->flags, flag_bits);
1417 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
1423 * Copy client info, but ignore partition number, which we
1424 * already got from phyp - unless we failed to get it from
1425 * phyp (e.g. if we're running on a p5 system).
1427 if (vscsi->client_data.partition_number == 0)
1428 vscsi->client_data.partition_number =
1429 be32_to_cpu(info->partition_number);
1430 strncpy(vscsi->client_data.srp_version, info->srp_version,
1431 sizeof(vscsi->client_data.srp_version));
1432 strncpy(vscsi->client_data.partition_name, info->partition_name,
1433 sizeof(vscsi->client_data.partition_name));
1434 vscsi->client_data.mad_version = be32_to_cpu(info->mad_version);
1435 vscsi->client_data.os_type = be32_to_cpu(info->os_type);
1438 strncpy(info->srp_version, SRP_VERSION,
1439 sizeof(info->srp_version));
1440 strncpy(info->partition_name, vscsi->dds.partition_name,
1441 sizeof(info->partition_name));
1442 info->partition_number = cpu_to_be32(vscsi->dds.partition_num);
1443 info->mad_version = cpu_to_be32(MAD_VERSION_1);
1444 info->os_type = cpu_to_be32(LINUX);
1445 memset(&info->port_max_txu[0], 0, sizeof(info->port_max_txu));
1446 info->port_max_txu[0] = cpu_to_be32(128 * PAGE_SIZE);
1449 rc = h_copy_rdma(sizeof(*info), vscsi->dds.window[LOCAL].liobn,
1450 token, vscsi->dds.window[REMOTE].liobn,
1451 be64_to_cpu(mad->buffer));
1459 if (connection_broken(vscsi))
1460 flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
1462 dev_err(&vscsi->dev, "adapter_info: h_copy_rdma to client failed, rc %ld\n",
1464 ibmvscsis_post_disconnect(vscsi,
1465 ERR_DISCONNECT_RECONNECT,
1471 dma_free_coherent(&vscsi->dma_dev->dev, sizeof(*info), info, token);
1472 pr_debug("Leaving adapter_info, rc %ld\n", rc);
1478 * ibmvscsis_cap_mad() - Service a Capabilities MAnagement Data gram
1479 * @vscsi: Pointer to our adapter structure
1480 * @iue: Information Unit containing the Capabilities MAD request
1482 * NOTE: if you return an error from this routine you must be
1483 * disconnecting or you will cause a hang
1485 * EXECUTION ENVIRONMENT:
1486 * Interrupt called with adapter lock held
1488 static int ibmvscsis_cap_mad(struct scsi_info *vscsi, struct iu_entry *iue)
1490 struct viosrp_capabilities *mad = &vio_iu(iue)->mad.capabilities;
1491 struct capabilities *cap;
1492 struct mad_capability_common *common;
1494 u16 olen, len, status, min_len, cap_len;
1499 olen = be16_to_cpu(mad->common.length);
1501 * struct capabilities hardcodes a couple capabilities after the
1502 * header, but the capabilities can actually be in any order.
1504 min_len = offsetof(struct capabilities, migration);
1505 if ((olen < min_len) || (olen > PAGE_SIZE)) {
1506 pr_warn("cap_mad: invalid len %d\n", olen);
1507 mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1511 cap = dma_alloc_coherent(&vscsi->dma_dev->dev, olen, &token,
1514 dev_err(&vscsi->dev, "bad dma_alloc_coherent %p\n",
1516 mad->common.status = cpu_to_be16(VIOSRP_MAD_FAILED);
1519 rc = h_copy_rdma(olen, vscsi->dds.window[REMOTE].liobn,
1520 be64_to_cpu(mad->buffer),
1521 vscsi->dds.window[LOCAL].liobn, token);
1522 if (rc == H_SUCCESS) {
1523 strncpy(cap->name, dev_name(&vscsi->dma_dev->dev),
1526 len = olen - min_len;
1527 status = VIOSRP_MAD_SUCCESS;
1528 common = (struct mad_capability_common *)&cap->migration;
1530 while ((len > 0) && (status == VIOSRP_MAD_SUCCESS) && !rc) {
1531 pr_debug("cap_mad: len left %hd, cap type %d, cap len %hd\n",
1532 len, be32_to_cpu(common->cap_type),
1533 be16_to_cpu(common->length));
1535 cap_len = be16_to_cpu(common->length);
1536 if (cap_len > len) {
1537 dev_err(&vscsi->dev, "cap_mad: cap len mismatch with total len\n");
1538 status = VIOSRP_MAD_FAILED;
1543 dev_err(&vscsi->dev, "cap_mad: cap len is 0\n");
1544 status = VIOSRP_MAD_FAILED;
1548 switch (common->cap_type) {
1550 pr_debug("cap_mad: unsupported capability\n");
1551 common->server_support = 0;
1552 flag = cpu_to_be32((u32)CAP_LIST_SUPPORTED);
1553 cap->flags &= ~flag;
1557 len = len - cap_len;
1558 common = (struct mad_capability_common *)
1559 ((char *)common + cap_len);
1562 mad->common.status = cpu_to_be16(status);
1565 rc = h_copy_rdma(olen, vscsi->dds.window[LOCAL].liobn, token,
1566 vscsi->dds.window[REMOTE].liobn,
1567 be64_to_cpu(mad->buffer));
1569 if (rc != H_SUCCESS) {
1570 pr_debug("cap_mad: failed to copy to client, rc %ld\n",
1573 if (rc == H_PERMISSION) {
1574 if (connection_broken(vscsi))
1575 flag_bits = (RESPONSE_Q_DOWN |
1579 pr_warn("cap_mad: error copying data to client, rc %ld\n",
1581 ibmvscsis_post_disconnect(vscsi,
1582 ERR_DISCONNECT_RECONNECT,
1587 dma_free_coherent(&vscsi->dma_dev->dev, olen, cap, token);
1589 pr_debug("Leaving cap_mad, rc %ld, client_cap 0x%x\n",
1590 rc, vscsi->client_cap);
1596 * ibmvscsis_process_mad() - Service a MAnagement Data gram
1597 * @vscsi: Pointer to our adapter structure
1598 * @iue: Information Unit containing the MAD request
1600 * Must be called with interrupt lock held.
1602 static long ibmvscsis_process_mad(struct scsi_info *vscsi, struct iu_entry *iue)
1604 struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
1605 struct viosrp_empty_iu *empty;
1606 long rc = ADAPT_SUCCESS;
1608 switch (be32_to_cpu(mad->type)) {
1609 case VIOSRP_EMPTY_IU_TYPE:
1610 empty = &vio_iu(iue)->mad.empty_iu;
1611 vscsi->empty_iu_id = be64_to_cpu(empty->buffer);
1612 vscsi->empty_iu_tag = be64_to_cpu(empty->common.tag);
1613 mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
1615 case VIOSRP_ADAPTER_INFO_TYPE:
1616 rc = ibmvscsis_adapter_info(vscsi, iue);
1618 case VIOSRP_CAPABILITIES_TYPE:
1619 rc = ibmvscsis_cap_mad(vscsi, iue);
1621 case VIOSRP_ENABLE_FAST_FAIL:
1622 if (vscsi->state == CONNECTED) {
1623 vscsi->fast_fail = true;
1624 mad->status = cpu_to_be16(VIOSRP_MAD_SUCCESS);
1626 pr_warn("fast fail mad sent after login\n");
1627 mad->status = cpu_to_be16(VIOSRP_MAD_FAILED);
1631 mad->status = cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED);
1639 * srp_snd_msg_failed() - Handle an error when sending a response
1640 * @vscsi: Pointer to our adapter structure
1641 * @rc: The return code from the h_send_crq command
1643 * Must be called with interrupt lock held.
1645 static void srp_snd_msg_failed(struct scsi_info *vscsi, long rc)
1649 if (rc != H_DROPPED) {
1650 ibmvscsis_free_cmd_qs(vscsi);
1653 vscsi->flags |= CLIENT_FAILED;
1655 /* don't flag the same problem multiple times */
1656 if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
1657 vscsi->flags |= RESPONSE_Q_DOWN;
1658 if (!(vscsi->state & (ERR_DISCONNECT |
1659 ERR_DISCONNECT_RECONNECT |
1660 ERR_DISCONNECTED | UNDEFINED))) {
1661 dev_err(&vscsi->dev, "snd_msg_failed: setting RESPONSE_Q_DOWN, state 0x%hx, flags 0x%x, rc %ld\n",
1662 vscsi->state, vscsi->flags, rc);
1664 ibmvscsis_post_disconnect(vscsi,
1665 ERR_DISCONNECT_RECONNECT, 0);
1671 * The response queue is full.
1672 * If the server is processing SRP requests, i.e.
1673 * the client has successfully done an
1674 * SRP_LOGIN, then it will wait forever for room in
1675 * the queue. However if the system admin
1676 * is attempting to unconfigure the server then one
1677 * or more children will be in a state where
1678 * they are being removed. So if there is even one
1679 * child being removed then the driver assumes
1680 * the system admin is attempting to break the
1681 * connection with the client and MAX_TIMER_POPS
1684 if ((vscsi->rsp_q_timer.timer_pops < MAX_TIMER_POPS) ||
1685 (vscsi->state == SRP_PROCESSING)) {
1686 pr_debug("snd_msg_failed: response queue full, flags 0x%x, timer started %d, pops %d\n",
1687 vscsi->flags, (int)vscsi->rsp_q_timer.started,
1688 vscsi->rsp_q_timer.timer_pops);
1691 * Check if the timer is running; if it
1692 * is not then start it up.
1694 if (!vscsi->rsp_q_timer.started) {
1695 if (vscsi->rsp_q_timer.timer_pops <
1697 kt = WAIT_NANO_SECONDS;
1700 * slide the timeslice if the maximum
1701 * timer pops have already happened
1703 kt = ktime_set(WAIT_SECONDS, 0);
1706 vscsi->rsp_q_timer.started = true;
1707 hrtimer_start(&vscsi->rsp_q_timer.timer, kt,
1712 * TBD: Do we need to worry about this? Need to get
1716 * waited a long time and it appears the system admin
1717 * is bring this driver down
1719 vscsi->flags |= RESPONSE_Q_DOWN;
1720 ibmvscsis_free_cmd_qs(vscsi);
1722 * if the driver is already attempting to disconnect
1723 * from the client and has already logged an error
1724 * trace this event but don't put it in the error log
1726 if (!(vscsi->state & (ERR_DISCONNECT |
1727 ERR_DISCONNECT_RECONNECT |
1728 ERR_DISCONNECTED | UNDEFINED))) {
1729 dev_err(&vscsi->dev, "client crq full too long\n");
1730 ibmvscsis_post_disconnect(vscsi,
1731 ERR_DISCONNECT_RECONNECT,
1738 * ibmvscsis_send_messages() - Send a Response
1739 * @vscsi: Pointer to our adapter structure
1741 * Send a response, first checking the waiting queue. Responses are
1742 * sent in order they are received. If the response cannot be sent,
1743 * because the client queue is full, it stays on the waiting queue.
1746 * Called with interrupt lock held
1748 static void ibmvscsis_send_messages(struct scsi_info *vscsi)
1751 /* note do not attmempt to access the IU_data_ptr with this pointer
1754 struct viosrp_crq *crq = (struct viosrp_crq *)&msg_hi;
1755 struct ibmvscsis_cmd *cmd, *nxt;
1756 struct iu_entry *iue;
1757 long rc = ADAPT_SUCCESS;
1759 if (!(vscsi->flags & RESPONSE_Q_DOWN)) {
1760 list_for_each_entry_safe(cmd, nxt, &vscsi->waiting_rsp, list) {
1763 crq->valid = VALID_CMD_RESP_EL;
1764 crq->format = cmd->rsp.format;
1766 if (cmd->flags & CMD_FAST_FAIL)
1767 crq->status = VIOSRP_ADAPTER_FAIL;
1769 crq->IU_length = cpu_to_be16(cmd->rsp.len);
1771 rc = h_send_crq(vscsi->dma_dev->unit_address,
1772 be64_to_cpu(msg_hi),
1773 be64_to_cpu(cmd->rsp.tag));
1775 pr_debug("send_messages: cmd %p, tag 0x%llx, rc %ld\n",
1776 cmd, be64_to_cpu(cmd->rsp.tag), rc);
1778 /* if all ok free up the command element resources */
1779 if (rc == H_SUCCESS) {
1780 /* some movement has occurred */
1781 vscsi->rsp_q_timer.timer_pops = 0;
1782 list_del(&cmd->list);
1784 ibmvscsis_free_cmd_resources(vscsi, cmd);
1786 srp_snd_msg_failed(vscsi, rc);
1793 * The timer could pop with the queue empty. If
1794 * this happens, rc will always indicate a
1795 * success; clear the pop count.
1797 vscsi->rsp_q_timer.timer_pops = 0;
1800 ibmvscsis_free_cmd_qs(vscsi);
1804 /* Called with intr lock held */
1805 static void ibmvscsis_send_mad_resp(struct scsi_info *vscsi,
1806 struct ibmvscsis_cmd *cmd,
1807 struct viosrp_crq *crq)
1809 struct iu_entry *iue = cmd->iue;
1810 struct mad_common *mad = (struct mad_common *)&vio_iu(iue)->mad;
1815 rc = h_copy_rdma(sizeof(struct mad_common),
1816 vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
1817 vscsi->dds.window[REMOTE].liobn,
1818 be64_to_cpu(crq->IU_data_ptr));
1820 cmd->rsp.format = VIOSRP_MAD_FORMAT;
1821 cmd->rsp.len = sizeof(struct mad_common);
1822 cmd->rsp.tag = mad->tag;
1823 list_add_tail(&cmd->list, &vscsi->waiting_rsp);
1824 ibmvscsis_send_messages(vscsi);
1826 pr_debug("Error sending mad response, rc %ld\n", rc);
1827 if (rc == H_PERMISSION) {
1828 if (connection_broken(vscsi))
1829 flag_bits = (RESPONSE_Q_DOWN | CLIENT_FAILED);
1831 dev_err(&vscsi->dev, "mad: failed to copy to client, rc %ld\n",
1834 ibmvscsis_free_cmd_resources(vscsi, cmd);
1835 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
1841 * ibmvscsis_mad() - Service a MAnagement Data gram.
1842 * @vscsi: Pointer to our adapter structure
1843 * @crq: Pointer to the CRQ entry containing the MAD request
1845 * EXECUTION ENVIRONMENT:
1846 * Interrupt, called with adapter lock held
1848 static long ibmvscsis_mad(struct scsi_info *vscsi, struct viosrp_crq *crq)
1850 struct iu_entry *iue;
1851 struct ibmvscsis_cmd *cmd;
1852 struct mad_common *mad;
1853 long rc = ADAPT_SUCCESS;
1855 switch (vscsi->state) {
1857 * We have not exchanged Init Msgs yet, so this MAD was sent
1858 * before the last Transport Event; client will not be
1859 * expecting a response.
1861 case WAIT_CONNECTION:
1862 pr_debug("mad: in Wait Connection state, ignoring MAD, flags %d\n",
1864 return ADAPT_SUCCESS;
1866 case SRP_PROCESSING:
1871 * We should never get here while we're in these states.
1872 * Just log an error and get out.
1876 case ERR_DISCONNECT:
1877 case ERR_DISCONNECT_RECONNECT:
1879 dev_err(&vscsi->dev, "mad: invalid adapter state %d for mad\n",
1881 return ADAPT_SUCCESS;
1884 cmd = ibmvscsis_get_free_cmd(vscsi);
1886 dev_err(&vscsi->dev, "mad: failed to get cmd, debit %d\n",
1888 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1892 cmd->type = ADAPTER_MAD;
1894 rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
1896 mad = (struct mad_common *)&vio_iu(iue)->mad;
1898 pr_debug("mad: type %d\n", be32_to_cpu(mad->type));
1900 rc = ibmvscsis_process_mad(vscsi, iue);
1902 pr_debug("mad: status %hd, rc %ld\n", be16_to_cpu(mad->status),
1906 ibmvscsis_send_mad_resp(vscsi, cmd, crq);
1908 ibmvscsis_free_cmd_resources(vscsi, cmd);
1911 pr_debug("Leaving mad, rc %ld\n", rc);
1916 * ibmvscsis_login_rsp() - Create/copy a login response notice to the client
1917 * @vscsi: Pointer to our adapter structure
1918 * @cmd: Pointer to the command for the SRP Login request
1920 * EXECUTION ENVIRONMENT:
1921 * Interrupt, interrupt lock held
1923 static long ibmvscsis_login_rsp(struct scsi_info *vscsi,
1924 struct ibmvscsis_cmd *cmd)
1926 struct iu_entry *iue = cmd->iue;
1927 struct srp_login_rsp *rsp = &vio_iu(iue)->srp.login_rsp;
1928 struct format_code *fmt;
1930 long rc = ADAPT_SUCCESS;
1932 memset(rsp, 0, sizeof(struct srp_login_rsp));
1934 rsp->opcode = SRP_LOGIN_RSP;
1935 rsp->req_lim_delta = cpu_to_be32(vscsi->request_limit);
1936 rsp->tag = cmd->rsp.tag;
1937 rsp->max_it_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
1938 rsp->max_ti_iu_len = cpu_to_be32(SRP_MAX_IU_LEN);
1939 fmt = (struct format_code *)&rsp->buf_fmt;
1940 fmt->buffers = SUPPORTED_FORMATS;
1943 cmd->rsp.len = sizeof(struct srp_login_rsp);
1946 rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
1947 iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
1948 be64_to_cpu(iue->remote_token));
1955 if (connection_broken(vscsi))
1956 flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
1957 dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
1959 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
1965 dev_err(&vscsi->dev, "login_rsp: error copying to client, rc %ld\n",
1967 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
1975 * ibmvscsis_srp_login_rej() - Create/copy a login rejection notice to client
1976 * @vscsi: Pointer to our adapter structure
1977 * @cmd: Pointer to the command for the SRP Login request
1978 * @reason: The reason the SRP Login is being rejected, per SRP protocol
1980 * EXECUTION ENVIRONMENT:
1981 * Interrupt, interrupt lock held
1983 static long ibmvscsis_srp_login_rej(struct scsi_info *vscsi,
1984 struct ibmvscsis_cmd *cmd, u32 reason)
1986 struct iu_entry *iue = cmd->iue;
1987 struct srp_login_rej *rej = &vio_iu(iue)->srp.login_rej;
1988 struct format_code *fmt;
1990 long rc = ADAPT_SUCCESS;
1992 memset(rej, 0, sizeof(*rej));
1994 rej->opcode = SRP_LOGIN_REJ;
1995 rej->reason = cpu_to_be32(reason);
1996 rej->tag = cmd->rsp.tag;
1997 fmt = (struct format_code *)&rej->buf_fmt;
1998 fmt->buffers = SUPPORTED_FORMATS;
2000 cmd->rsp.len = sizeof(*rej);
2003 rc = h_copy_rdma(cmd->rsp.len, vscsi->dds.window[LOCAL].liobn,
2004 iue->sbuf->dma, vscsi->dds.window[REMOTE].liobn,
2005 be64_to_cpu(iue->remote_token));
2011 if (connection_broken(vscsi))
2012 flag_bits = RESPONSE_Q_DOWN | CLIENT_FAILED;
2013 dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
2015 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT,
2021 dev_err(&vscsi->dev, "login_rej: error copying to client, rc %ld\n",
2023 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2030 static int ibmvscsis_make_nexus(struct ibmvscsis_tport *tport)
2032 char *name = tport->tport_name;
2033 struct ibmvscsis_nexus *nexus;
2036 if (tport->ibmv_nexus) {
2037 pr_debug("tport->ibmv_nexus already exists\n");
2041 nexus = kzalloc(sizeof(*nexus), GFP_KERNEL);
2043 pr_err("Unable to allocate struct ibmvscsis_nexus\n");
2047 nexus->se_sess = target_alloc_session(&tport->se_tpg, 0, 0,
2048 TARGET_PROT_NORMAL, name, nexus,
2050 if (IS_ERR(nexus->se_sess)) {
2051 rc = PTR_ERR(nexus->se_sess);
2052 goto transport_init_fail;
2055 tport->ibmv_nexus = nexus;
2059 transport_init_fail:
2064 static int ibmvscsis_drop_nexus(struct ibmvscsis_tport *tport)
2066 struct se_session *se_sess;
2067 struct ibmvscsis_nexus *nexus;
2069 nexus = tport->ibmv_nexus;
2073 se_sess = nexus->se_sess;
2078 * Release the SCSI I_T Nexus to the emulated ibmvscsis Target Port
2080 target_wait_for_sess_cmds(se_sess);
2081 transport_deregister_session_configfs(se_sess);
2082 transport_deregister_session(se_sess);
2083 tport->ibmv_nexus = NULL;
2090 * ibmvscsis_srp_login() - Process an SRP Login Request
2091 * @vscsi: Pointer to our adapter structure
2092 * @cmd: Command element to use to process the SRP Login request
2093 * @crq: Pointer to CRQ entry containing the SRP Login request
2095 * EXECUTION ENVIRONMENT:
2096 * Interrupt, called with interrupt lock held
2098 static long ibmvscsis_srp_login(struct scsi_info *vscsi,
2099 struct ibmvscsis_cmd *cmd,
2100 struct viosrp_crq *crq)
2102 struct iu_entry *iue = cmd->iue;
2103 struct srp_login_req *req = &vio_iu(iue)->srp.login_req;
2105 __be64 id_extension;
2108 struct format_code *fmt;
2110 long rc = ADAPT_SUCCESS;
2112 iport = (struct port_id *)req->initiator_port_id;
2113 tport = (struct port_id *)req->target_port_id;
2114 fmt = (struct format_code *)&req->req_buf_fmt;
2115 if (be32_to_cpu(req->req_it_iu_len) > SRP_MAX_IU_LEN)
2116 reason = SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE;
2117 else if (be32_to_cpu(req->req_it_iu_len) < 64)
2118 reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
2119 else if ((be64_to_cpu(iport->id_extension) > (MAX_NUM_PORTS - 1)) ||
2120 (be64_to_cpu(tport->id_extension) > (MAX_NUM_PORTS - 1)))
2121 reason = SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL;
2122 else if (req->req_flags & SRP_MULTICHAN_MULTI)
2123 reason = SRP_LOGIN_REJ_MULTI_CHANNEL_UNSUPPORTED;
2124 else if (fmt->buffers & (~SUPPORTED_FORMATS))
2125 reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
2126 else if ((fmt->buffers & SUPPORTED_FORMATS) == 0)
2127 reason = SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT;
2129 if (vscsi->state == SRP_PROCESSING)
2130 reason = SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED;
2132 rc = ibmvscsis_make_nexus(&vscsi->tport);
2134 reason = SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL;
2136 cmd->rsp.format = VIOSRP_SRP_FORMAT;
2137 cmd->rsp.tag = req->tag;
2139 pr_debug("srp_login: reason 0x%x\n", reason);
2142 rc = ibmvscsis_srp_login_rej(vscsi, cmd, reason);
2144 rc = ibmvscsis_login_rsp(vscsi, cmd);
2148 vscsi->state = SRP_PROCESSING;
2150 list_add_tail(&cmd->list, &vscsi->waiting_rsp);
2151 ibmvscsis_send_messages(vscsi);
2153 ibmvscsis_free_cmd_resources(vscsi, cmd);
2156 pr_debug("Leaving srp_login, rc %ld\n", rc);
2161 * ibmvscsis_srp_i_logout() - Helper Function to close I_T Nexus
2162 * @vscsi: Pointer to our adapter structure
2163 * @cmd: Command element to use to process the Implicit Logout request
2164 * @crq: Pointer to CRQ entry containing the Implicit Logout request
2166 * Do the logic to close the I_T nexus. This function may not
2167 * behave to specification.
2169 * EXECUTION ENVIRONMENT:
2170 * Interrupt, interrupt lock held
2172 static long ibmvscsis_srp_i_logout(struct scsi_info *vscsi,
2173 struct ibmvscsis_cmd *cmd,
2174 struct viosrp_crq *crq)
2176 struct iu_entry *iue = cmd->iue;
2177 struct srp_i_logout *log_out = &vio_iu(iue)->srp.i_logout;
2178 long rc = ADAPT_SUCCESS;
2180 if ((vscsi->debit > 0) || !list_empty(&vscsi->schedule_q) ||
2181 !list_empty(&vscsi->waiting_rsp)) {
2182 dev_err(&vscsi->dev, "i_logout: outstanding work\n");
2183 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
2185 cmd->rsp.format = SRP_FORMAT;
2186 cmd->rsp.tag = log_out->tag;
2187 cmd->rsp.len = sizeof(struct mad_common);
2188 list_add_tail(&cmd->list, &vscsi->waiting_rsp);
2189 ibmvscsis_send_messages(vscsi);
2191 ibmvscsis_post_disconnect(vscsi, WAIT_IDLE, 0);
2197 /* Called with intr lock held */
2198 static void ibmvscsis_srp_cmd(struct scsi_info *vscsi, struct viosrp_crq *crq)
2200 struct ibmvscsis_cmd *cmd;
2201 struct iu_entry *iue;
2202 struct srp_cmd *srp;
2203 struct srp_tsk_mgmt *tsk;
2206 if (vscsi->request_limit - vscsi->debit <= 0) {
2207 /* Client has exceeded request limit */
2208 dev_err(&vscsi->dev, "Client exceeded the request limit (%d), debit %d\n",
2209 vscsi->request_limit, vscsi->debit);
2210 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2214 cmd = ibmvscsis_get_free_cmd(vscsi);
2216 dev_err(&vscsi->dev, "srp_cmd failed to get cmd, debit %d\n",
2218 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2222 srp = &vio_iu(iue)->srp.cmd;
2224 rc = ibmvscsis_copy_crq_packet(vscsi, cmd, crq);
2226 ibmvscsis_free_cmd_resources(vscsi, cmd);
2230 if (vscsi->state == SRP_PROCESSING) {
2231 switch (srp->opcode) {
2233 rc = ibmvscsis_srp_login(vscsi, cmd, crq);
2237 tsk = &vio_iu(iue)->srp.tsk_mgmt;
2238 pr_debug("tsk_mgmt tag: %llu (0x%llx)\n", tsk->tag,
2240 cmd->rsp.tag = tsk->tag;
2242 cmd->type = TASK_MANAGEMENT;
2243 list_add_tail(&cmd->list, &vscsi->schedule_q);
2244 queue_work(vscsi->work_q, &cmd->work);
2248 pr_debug("srp_cmd tag: %llu (0x%llx)\n", srp->tag,
2250 cmd->rsp.tag = srp->tag;
2252 cmd->type = SCSI_CDB;
2254 * We want to keep track of work waiting for
2257 list_add_tail(&cmd->list, &vscsi->schedule_q);
2258 queue_work(vscsi->work_q, &cmd->work);
2262 rc = ibmvscsis_srp_i_logout(vscsi, cmd, crq);
2268 ibmvscsis_free_cmd_resources(vscsi, cmd);
2269 dev_err(&vscsi->dev, "invalid srp cmd, opcode %d\n",
2271 ibmvscsis_post_disconnect(vscsi,
2272 ERR_DISCONNECT_RECONNECT, 0);
2275 } else if (srp->opcode == SRP_LOGIN_REQ && vscsi->state == CONNECTED) {
2276 rc = ibmvscsis_srp_login(vscsi, cmd, crq);
2278 ibmvscsis_free_cmd_resources(vscsi, cmd);
2279 dev_err(&vscsi->dev, "Invalid state %d to handle srp cmd\n",
2281 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2286 * ibmvscsis_ping_response() - Respond to a ping request
2287 * @vscsi: Pointer to our adapter structure
2289 * Let the client know that the server is alive and waiting on
2290 * its native I/O stack.
2291 * If any type of error occurs from the call to queue a ping
2292 * response then the client is either not accepting or receiving
2293 * interrupts. Disconnect with an error.
2295 * EXECUTION ENVIRONMENT:
2296 * Interrupt, interrupt lock held
2298 static long ibmvscsis_ping_response(struct scsi_info *vscsi)
2300 struct viosrp_crq *crq;
2301 u64 buffer[2] = { 0, 0 };
2304 crq = (struct viosrp_crq *)&buffer;
2305 crq->valid = VALID_CMD_RESP_EL;
2306 crq->format = (u8)MESSAGE_IN_CRQ;
2307 crq->status = PING_RESPONSE;
2309 rc = h_send_crq(vscsi->dds.unit_id, cpu_to_be64(buffer[MSG_HI]),
2310 cpu_to_be64(buffer[MSG_LOW]));
2316 vscsi->flags |= CLIENT_FAILED;
2318 vscsi->flags |= RESPONSE_Q_DOWN;
2320 dev_err(&vscsi->dev, "ping_response: h_send_crq failed, rc %ld\n",
2322 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2325 dev_err(&vscsi->dev, "ping_response: h_send_crq returned unknown rc %ld\n",
2327 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
2335 * ibmvscsis_parse_command() - Parse an element taken from the cmd rsp queue.
2336 * @vscsi: Pointer to our adapter structure
2337 * @crq: Pointer to CRQ element containing the SRP request
2339 * This function will return success if the command queue element is valid
2340 * and the srp iu or MAD request it pointed to was also valid. That does
2341 * not mean that an error was not returned to the client.
2343 * EXECUTION ENVIRONMENT:
2344 * Interrupt, intr lock held
2346 static long ibmvscsis_parse_command(struct scsi_info *vscsi,
2347 struct viosrp_crq *crq)
2349 long rc = ADAPT_SUCCESS;
2351 switch (crq->valid) {
2352 case VALID_CMD_RESP_EL:
2353 switch (crq->format) {
2358 if (vscsi->flags & PROCESSING_MAD) {
2360 dev_err(&vscsi->dev, "parse_command: already processing mad\n");
2361 ibmvscsis_post_disconnect(vscsi,
2362 ERR_DISCONNECT_RECONNECT,
2365 vscsi->flags |= PROCESSING_MAD;
2366 rc = ibmvscsis_mad(vscsi, crq);
2371 ibmvscsis_srp_cmd(vscsi, crq);
2374 case MESSAGE_IN_CRQ:
2375 if (crq->status == PING)
2376 ibmvscsis_ping_response(vscsi);
2380 dev_err(&vscsi->dev, "parse_command: invalid format %d\n",
2382 ibmvscsis_post_disconnect(vscsi,
2383 ERR_DISCONNECT_RECONNECT, 0);
2388 case VALID_TRANS_EVENT:
2389 rc = ibmvscsis_trans_event(vscsi, crq);
2392 case VALID_INIT_MSG:
2393 rc = ibmvscsis_init_msg(vscsi, crq);
2397 dev_err(&vscsi->dev, "parse_command: invalid valid field %d\n",
2399 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2404 * Return only what the interrupt handler cares
2405 * about. Most errors we keep right on trucking.
2407 rc = vscsi->flags & SCHEDULE_DISCONNECT;
2412 static int read_dma_window(struct scsi_info *vscsi)
2414 struct vio_dev *vdev = vscsi->dma_dev;
2415 const __be32 *dma_window;
2418 /* TODO Using of_parse_dma_window would be better, but it doesn't give
2419 * a way to read multiple windows without already knowing the size of
2420 * a window or the number of windows.
2422 dma_window = (const __be32 *)vio_get_attribute(vdev,
2423 "ibm,my-dma-window",
2426 pr_err("Couldn't find ibm,my-dma-window property\n");
2430 vscsi->dds.window[LOCAL].liobn = be32_to_cpu(*dma_window);
2433 prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-address-cells",
2436 pr_warn("Couldn't find ibm,#dma-address-cells property\n");
2439 dma_window += be32_to_cpu(*prop);
2442 prop = (const __be32 *)vio_get_attribute(vdev, "ibm,#dma-size-cells",
2445 pr_warn("Couldn't find ibm,#dma-size-cells property\n");
2448 dma_window += be32_to_cpu(*prop);
2451 /* dma_window should point to the second window now */
2452 vscsi->dds.window[REMOTE].liobn = be32_to_cpu(*dma_window);
2457 static struct ibmvscsis_tport *ibmvscsis_lookup_port(const char *name)
2459 struct ibmvscsis_tport *tport = NULL;
2460 struct vio_dev *vdev;
2461 struct scsi_info *vscsi;
2463 spin_lock_bh(&ibmvscsis_dev_lock);
2464 list_for_each_entry(vscsi, &ibmvscsis_dev_list, list) {
2465 vdev = vscsi->dma_dev;
2466 if (!strcmp(dev_name(&vdev->dev), name)) {
2467 tport = &vscsi->tport;
2471 spin_unlock_bh(&ibmvscsis_dev_lock);
2477 * ibmvscsis_parse_cmd() - Parse SRP Command
2478 * @vscsi: Pointer to our adapter structure
2479 * @cmd: Pointer to command element with SRP command
2481 * Parse the srp command; if it is valid then submit it to tcm.
2482 * Note: The return code does not reflect the status of the SCSI CDB.
2484 * EXECUTION ENVIRONMENT:
2487 static void ibmvscsis_parse_cmd(struct scsi_info *vscsi,
2488 struct ibmvscsis_cmd *cmd)
2490 struct iu_entry *iue = cmd->iue;
2491 struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
2492 struct ibmvscsis_nexus *nexus;
2494 enum dma_data_direction dir;
2498 nexus = vscsi->tport.ibmv_nexus;
2500 * additional length in bytes. Note that the SRP spec says that
2501 * additional length is in 4-byte words, but technically the
2502 * additional length field is only the upper 6 bits of the byte.
2503 * The lower 2 bits are reserved. If the lower 2 bits are 0 (as
2504 * all reserved fields should be), then interpreting the byte as
2505 * an int will yield the length in bytes.
2507 if (srp->add_cdb_len & 0x03) {
2508 dev_err(&vscsi->dev, "parse_cmd: reserved bits set in IU\n");
2509 spin_lock_bh(&vscsi->intr_lock);
2510 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2511 ibmvscsis_free_cmd_resources(vscsi, cmd);
2512 spin_unlock_bh(&vscsi->intr_lock);
2516 if (srp_get_desc_table(srp, &dir, &data_len)) {
2517 dev_err(&vscsi->dev, "0x%llx: parsing SRP descriptor table failed.\n",
2522 cmd->rsp.sol_not = srp->sol_not;
2524 switch (srp->task_attr) {
2525 case SRP_SIMPLE_TASK:
2526 attr = TCM_SIMPLE_TAG;
2528 case SRP_ORDERED_TASK:
2529 attr = TCM_ORDERED_TAG;
2532 attr = TCM_HEAD_TAG;
2538 dev_err(&vscsi->dev, "Invalid task attribute %d\n",
2543 cmd->se_cmd.tag = be64_to_cpu(srp->tag);
2545 spin_lock_bh(&vscsi->intr_lock);
2546 list_add_tail(&cmd->list, &vscsi->active_q);
2547 spin_unlock_bh(&vscsi->intr_lock);
2549 srp->lun.scsi_lun[0] &= 0x3f;
2551 rc = target_submit_cmd(&cmd->se_cmd, nexus->se_sess, srp->cdb,
2552 cmd->sense_buf, scsilun_to_int(&srp->lun),
2553 data_len, attr, dir, 0);
2555 dev_err(&vscsi->dev, "target_submit_cmd failed, rc %d\n", rc);
2556 spin_lock_bh(&vscsi->intr_lock);
2557 list_del(&cmd->list);
2558 ibmvscsis_free_cmd_resources(vscsi, cmd);
2559 spin_unlock_bh(&vscsi->intr_lock);
2565 spin_lock_bh(&vscsi->intr_lock);
2566 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT_RECONNECT, 0);
2567 spin_unlock_bh(&vscsi->intr_lock);
2571 * ibmvscsis_parse_task() - Parse SRP Task Management Request
2572 * @vscsi: Pointer to our adapter structure
2573 * @cmd: Pointer to command element with SRP task management request
2575 * Parse the srp task management request; if it is valid then submit it to tcm.
2576 * Note: The return code does not reflect the status of the task management
2579 * EXECUTION ENVIRONMENT:
2582 static void ibmvscsis_parse_task(struct scsi_info *vscsi,
2583 struct ibmvscsis_cmd *cmd)
2585 struct iu_entry *iue = cmd->iue;
2586 struct srp_tsk_mgmt *srp_tsk = &vio_iu(iue)->srp.tsk_mgmt;
2588 u64 tag_to_abort = 0;
2590 struct ibmvscsis_nexus *nexus;
2592 nexus = vscsi->tport.ibmv_nexus;
2594 cmd->rsp.sol_not = srp_tsk->sol_not;
2596 switch (srp_tsk->tsk_mgmt_func) {
2597 case SRP_TSK_ABORT_TASK:
2598 tcm_type = TMR_ABORT_TASK;
2599 tag_to_abort = be64_to_cpu(srp_tsk->task_tag);
2601 case SRP_TSK_ABORT_TASK_SET:
2602 tcm_type = TMR_ABORT_TASK_SET;
2604 case SRP_TSK_CLEAR_TASK_SET:
2605 tcm_type = TMR_CLEAR_TASK_SET;
2607 case SRP_TSK_LUN_RESET:
2608 tcm_type = TMR_LUN_RESET;
2610 case SRP_TSK_CLEAR_ACA:
2611 tcm_type = TMR_CLEAR_ACA;
2614 dev_err(&vscsi->dev, "unknown task mgmt func %d\n",
2615 srp_tsk->tsk_mgmt_func);
2616 cmd->se_cmd.se_tmr_req->response =
2617 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED;
2623 cmd->se_cmd.tag = be64_to_cpu(srp_tsk->tag);
2625 spin_lock_bh(&vscsi->intr_lock);
2626 list_add_tail(&cmd->list, &vscsi->active_q);
2627 spin_unlock_bh(&vscsi->intr_lock);
2629 srp_tsk->lun.scsi_lun[0] &= 0x3f;
2631 pr_debug("calling submit_tmr, func %d\n",
2632 srp_tsk->tsk_mgmt_func);
2633 rc = target_submit_tmr(&cmd->se_cmd, nexus->se_sess, NULL,
2634 scsilun_to_int(&srp_tsk->lun), srp_tsk,
2635 tcm_type, GFP_KERNEL, tag_to_abort, 0);
2637 dev_err(&vscsi->dev, "target_submit_tmr failed, rc %d\n",
2639 spin_lock_bh(&vscsi->intr_lock);
2640 list_del(&cmd->list);
2641 spin_unlock_bh(&vscsi->intr_lock);
2642 cmd->se_cmd.se_tmr_req->response =
2643 TMR_FUNCTION_REJECTED;
2648 transport_send_check_condition_and_sense(&cmd->se_cmd, 0, 0);
2651 static void ibmvscsis_scheduler(struct work_struct *work)
2653 struct ibmvscsis_cmd *cmd = container_of(work, struct ibmvscsis_cmd,
2655 struct scsi_info *vscsi = cmd->adapter;
2657 spin_lock_bh(&vscsi->intr_lock);
2659 /* Remove from schedule_q */
2660 list_del(&cmd->list);
2662 /* Don't submit cmd if we're disconnecting */
2663 if (vscsi->flags & (SCHEDULE_DISCONNECT | DISCONNECT_SCHEDULED)) {
2664 ibmvscsis_free_cmd_resources(vscsi, cmd);
2666 /* ibmvscsis_disconnect might be waiting for us */
2667 if (list_empty(&vscsi->active_q) &&
2668 list_empty(&vscsi->schedule_q) &&
2669 (vscsi->flags & WAIT_FOR_IDLE)) {
2670 vscsi->flags &= ~WAIT_FOR_IDLE;
2671 complete(&vscsi->wait_idle);
2674 spin_unlock_bh(&vscsi->intr_lock);
2678 spin_unlock_bh(&vscsi->intr_lock);
2680 switch (cmd->type) {
2682 ibmvscsis_parse_cmd(vscsi, cmd);
2684 case TASK_MANAGEMENT:
2685 ibmvscsis_parse_task(vscsi, cmd);
2688 dev_err(&vscsi->dev, "scheduler, invalid cmd type %d\n",
2690 spin_lock_bh(&vscsi->intr_lock);
2691 ibmvscsis_free_cmd_resources(vscsi, cmd);
2692 spin_unlock_bh(&vscsi->intr_lock);
2697 static int ibmvscsis_alloc_cmds(struct scsi_info *vscsi, int num)
2699 struct ibmvscsis_cmd *cmd;
2702 INIT_LIST_HEAD(&vscsi->free_cmd);
2703 vscsi->cmd_pool = kcalloc(num, sizeof(struct ibmvscsis_cmd),
2705 if (!vscsi->cmd_pool)
2708 for (i = 0, cmd = (struct ibmvscsis_cmd *)vscsi->cmd_pool; i < num;
2710 cmd->adapter = vscsi;
2711 INIT_WORK(&cmd->work, ibmvscsis_scheduler);
2712 list_add_tail(&cmd->list, &vscsi->free_cmd);
2718 static void ibmvscsis_free_cmds(struct scsi_info *vscsi)
2720 kfree(vscsi->cmd_pool);
2721 vscsi->cmd_pool = NULL;
2722 INIT_LIST_HEAD(&vscsi->free_cmd);
2726 * ibmvscsis_service_wait_q() - Service Waiting Queue
2727 * @timer: Pointer to timer which has expired
2729 * This routine is called when the timer pops to service the waiting
2730 * queue. Elements on the queue have completed, their responses have been
2731 * copied to the client, but the client's response queue was full so
2732 * the queue message could not be sent. The routine grabs the proper locks
2733 * and calls send messages.
2735 * EXECUTION ENVIRONMENT:
2736 * called at interrupt level
2738 static enum hrtimer_restart ibmvscsis_service_wait_q(struct hrtimer *timer)
2740 struct timer_cb *p_timer = container_of(timer, struct timer_cb, timer);
2741 struct scsi_info *vscsi = container_of(p_timer, struct scsi_info,
2744 spin_lock_bh(&vscsi->intr_lock);
2745 p_timer->timer_pops += 1;
2746 p_timer->started = false;
2747 ibmvscsis_send_messages(vscsi);
2748 spin_unlock_bh(&vscsi->intr_lock);
2750 return HRTIMER_NORESTART;
2753 static long ibmvscsis_alloctimer(struct scsi_info *vscsi)
2755 struct timer_cb *p_timer;
2757 p_timer = &vscsi->rsp_q_timer;
2758 hrtimer_init(&p_timer->timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
2760 p_timer->timer.function = ibmvscsis_service_wait_q;
2761 p_timer->started = false;
2762 p_timer->timer_pops = 0;
2764 return ADAPT_SUCCESS;
2767 static void ibmvscsis_freetimer(struct scsi_info *vscsi)
2769 struct timer_cb *p_timer;
2771 p_timer = &vscsi->rsp_q_timer;
2773 (void)hrtimer_cancel(&p_timer->timer);
2775 p_timer->started = false;
2776 p_timer->timer_pops = 0;
2779 static irqreturn_t ibmvscsis_interrupt(int dummy, void *data)
2781 struct scsi_info *vscsi = data;
2783 vio_disable_interrupts(vscsi->dma_dev);
2784 tasklet_schedule(&vscsi->work_task);
2790 * ibmvscsis_enable_change_state() - Set new state based on enabled status
2791 * @vscsi: Pointer to our adapter structure
2793 * This function determines our new state now that we are enabled. This
2794 * may involve sending an Init Complete message to the client.
2796 * Must be called with interrupt lock held.
2798 static long ibmvscsis_enable_change_state(struct scsi_info *vscsi)
2801 long rc = ADAPT_SUCCESS;
2803 bytes = vscsi->cmd_q.size * PAGE_SIZE;
2804 rc = h_reg_crq(vscsi->dds.unit_id, vscsi->cmd_q.crq_token, bytes);
2805 if (rc == H_CLOSED || rc == H_SUCCESS) {
2806 vscsi->state = WAIT_CONNECTION;
2807 rc = ibmvscsis_establish_new_q(vscsi);
2810 if (rc != ADAPT_SUCCESS) {
2811 vscsi->state = ERR_DISCONNECTED;
2812 vscsi->flags |= RESPONSE_Q_DOWN;
2819 * ibmvscsis_create_command_q() - Create Command Queue
2820 * @vscsi: Pointer to our adapter structure
2821 * @num_cmds: Currently unused. In the future, may be used to determine
2822 * the size of the CRQ.
2824 * Allocates memory for command queue maps remote memory into an ioba
2825 * initializes the command response queue
2827 * EXECUTION ENVIRONMENT:
2828 * Process level only
2830 static long ibmvscsis_create_command_q(struct scsi_info *vscsi, int num_cmds)
2833 struct vio_dev *vdev = vscsi->dma_dev;
2835 /* We might support multiple pages in the future, but just 1 for now */
2838 vscsi->cmd_q.size = pages;
2840 vscsi->cmd_q.base_addr =
2841 (struct viosrp_crq *)get_zeroed_page(GFP_KERNEL);
2842 if (!vscsi->cmd_q.base_addr)
2845 vscsi->cmd_q.mask = ((uint)pages * CRQ_PER_PAGE) - 1;
2847 vscsi->cmd_q.crq_token = dma_map_single(&vdev->dev,
2848 vscsi->cmd_q.base_addr,
2849 PAGE_SIZE, DMA_BIDIRECTIONAL);
2850 if (dma_mapping_error(&vdev->dev, vscsi->cmd_q.crq_token)) {
2851 free_page((unsigned long)vscsi->cmd_q.base_addr);
2859 * ibmvscsis_destroy_command_q - Destroy Command Queue
2860 * @vscsi: Pointer to our adapter structure
2862 * Releases memory for command queue and unmaps mapped remote memory.
2864 * EXECUTION ENVIRONMENT:
2865 * Process level only
2867 static void ibmvscsis_destroy_command_q(struct scsi_info *vscsi)
2869 dma_unmap_single(&vscsi->dma_dev->dev, vscsi->cmd_q.crq_token,
2870 PAGE_SIZE, DMA_BIDIRECTIONAL);
2871 free_page((unsigned long)vscsi->cmd_q.base_addr);
2872 vscsi->cmd_q.base_addr = NULL;
2873 vscsi->state = NO_QUEUE;
2876 static u8 ibmvscsis_fast_fail(struct scsi_info *vscsi,
2877 struct ibmvscsis_cmd *cmd)
2879 struct iu_entry *iue = cmd->iue;
2880 struct se_cmd *se_cmd = &cmd->se_cmd;
2881 struct srp_cmd *srp = (struct srp_cmd *)iue->sbuf->buf;
2882 struct scsi_sense_hdr sshdr;
2883 u8 rc = se_cmd->scsi_status;
2885 if (vscsi->fast_fail && (READ_CMD(srp->cdb) || WRITE_CMD(srp->cdb)))
2886 if (scsi_normalize_sense(se_cmd->sense_buffer,
2887 se_cmd->scsi_sense_length, &sshdr))
2888 if (sshdr.sense_key == HARDWARE_ERROR &&
2889 (se_cmd->residual_count == 0 ||
2890 se_cmd->residual_count == se_cmd->data_length)) {
2892 cmd->flags |= CMD_FAST_FAIL;
2899 * srp_build_response() - Build an SRP response buffer
2900 * @vscsi: Pointer to our adapter structure
2901 * @cmd: Pointer to command for which to send the response
2902 * @len_p: Where to return the length of the IU response sent. This
2903 * is needed to construct the CRQ response.
2905 * Build the SRP response buffer and copy it to the client's memory space.
2907 static long srp_build_response(struct scsi_info *vscsi,
2908 struct ibmvscsis_cmd *cmd, uint *len_p)
2910 struct iu_entry *iue = cmd->iue;
2911 struct se_cmd *se_cmd = &cmd->se_cmd;
2912 struct srp_rsp *rsp;
2917 long rc = ADAPT_SUCCESS;
2919 spin_lock_bh(&vscsi->intr_lock);
2921 rsp = &vio_iu(iue)->srp.rsp;
2923 memset(rsp, 0, len);
2926 rsp->opcode = SRP_RSP;
2928 if (vscsi->credit > 0 && vscsi->state == SRP_PROCESSING)
2929 rsp->req_lim_delta = cpu_to_be32(vscsi->credit);
2931 rsp->req_lim_delta = cpu_to_be32(1 + vscsi->credit);
2932 rsp->tag = cmd->rsp.tag;
2935 if (cmd->type == SCSI_CDB) {
2936 rsp->status = ibmvscsis_fast_fail(vscsi, cmd);
2938 pr_debug("build_resp: cmd %p, scsi status %d\n", cmd,
2940 ibmvscsis_determine_resid(se_cmd, rsp);
2941 if (se_cmd->scsi_sense_length && se_cmd->sense_buffer) {
2942 rsp->sense_data_len =
2943 cpu_to_be32(se_cmd->scsi_sense_length);
2944 rsp->flags |= SRP_RSP_FLAG_SNSVALID;
2945 len += se_cmd->scsi_sense_length;
2946 memcpy(data, se_cmd->sense_buffer,
2947 se_cmd->scsi_sense_length);
2949 rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2951 } else if (cmd->flags & CMD_FAST_FAIL) {
2952 pr_debug("build_resp: cmd %p, fast fail\n", cmd);
2953 rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2956 rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
2960 /* this is task management */
2962 rsp->resp_data_len = cpu_to_be32(4);
2963 rsp->flags |= SRP_RSP_FLAG_RSPVALID;
2965 switch (se_cmd->se_tmr_req->response) {
2966 case TMR_FUNCTION_COMPLETE:
2967 case TMR_TASK_DOES_NOT_EXIST:
2968 rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_COMPLETE;
2969 rsp->sol_not = (cmd->rsp.sol_not & SCSOLNT) >>
2972 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED:
2973 case TMR_LUN_DOES_NOT_EXIST:
2974 rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_NOT_SUPPORTED;
2975 rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2978 case TMR_FUNCTION_FAILED:
2979 case TMR_FUNCTION_REJECTED:
2981 rsp_code = SRP_TASK_MANAGEMENT_FUNCTION_FAILED;
2982 rsp->sol_not = (cmd->rsp.sol_not & UCSOLNT) >>
2987 tsk_status = (u32 *)data;
2988 *tsk_status = cpu_to_be32(rsp_code);
2989 data = (char *)(tsk_status + 1);
2994 rc = h_copy_rdma(len, vscsi->dds.window[LOCAL].liobn, iue->sbuf->dma,
2995 vscsi->dds.window[REMOTE].liobn,
2996 be64_to_cpu(iue->remote_token));
3004 if (connection_broken(vscsi))
3005 vscsi->flags |= RESPONSE_Q_DOWN | CLIENT_FAILED;
3007 dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld, flags 0x%x, state 0x%hx\n",
3008 rc, vscsi->flags, vscsi->state);
3013 dev_err(&vscsi->dev, "build_response: error copying to client, rc %ld\n",
3018 spin_unlock_bh(&vscsi->intr_lock);
3023 static int ibmvscsis_rdma(struct ibmvscsis_cmd *cmd, struct scatterlist *sg,
3024 int nsg, struct srp_direct_buf *md, int nmd,
3025 enum dma_data_direction dir, unsigned int bytes)
3027 struct iu_entry *iue = cmd->iue;
3028 struct srp_target *target = iue->target;
3029 struct scsi_info *vscsi = target->ldata;
3030 struct scatterlist *sgp;
3031 dma_addr_t client_ioba, server_ioba;
3033 ulong client_len, server_len;
3048 if (client_len == 0) {
3049 if (md_idx >= nmd) {
3050 dev_err(&vscsi->dev, "rdma: ran out of client memory descriptors\n");
3054 client_ioba = be64_to_cpu(md[md_idx].va);
3055 client_len = be32_to_cpu(md[md_idx].len);
3057 if (server_len == 0) {
3059 dev_err(&vscsi->dev, "rdma: ran out of scatter/gather list\n");
3063 server_ioba = sg_dma_address(sgp);
3064 server_len = sg_dma_len(sgp);
3069 if (buf_len > client_len)
3070 buf_len = client_len;
3072 if (buf_len > server_len)
3073 buf_len = server_len;
3075 if (buf_len > max_vdma_size)
3076 buf_len = max_vdma_size;
3078 if (dir == DMA_TO_DEVICE) {
3079 /* read from client */
3080 rc = h_copy_rdma(buf_len,
3081 vscsi->dds.window[REMOTE].liobn,
3083 vscsi->dds.window[LOCAL].liobn,
3086 /* The h_copy_rdma will cause phyp, running in another
3087 * partition, to read memory, so we need to make sure
3088 * the data has been written out, hence these syncs.
3090 /* ensure that everything is in memory */
3092 /* ensure that memory has been made visible */
3094 rc = h_copy_rdma(buf_len,
3095 vscsi->dds.window[LOCAL].liobn,
3097 vscsi->dds.window[REMOTE].liobn,
3106 if (connection_broken(vscsi)) {
3107 spin_lock_bh(&vscsi->intr_lock);
3109 (RESPONSE_Q_DOWN | CLIENT_FAILED);
3110 spin_unlock_bh(&vscsi->intr_lock);
3112 dev_err(&vscsi->dev, "rdma: h_copy_rdma failed, rc %ld\n",
3117 dev_err(&vscsi->dev, "rdma: unknown error %ld from h_copy_rdma\n",
3125 client_len -= buf_len;
3126 if (client_len == 0)
3129 client_ioba += buf_len;
3131 server_len -= buf_len;
3132 if (server_len == 0)
3135 server_ioba += buf_len;
3146 * ibmvscsis_handle_crq() - Handle CRQ
3147 * @data: Pointer to our adapter structure
3149 * Read the command elements from the command queue and copy the payloads
3150 * associated with the command elements to local memory and execute the
3153 * Note: this is an edge triggered interrupt. It can not be shared.
3155 static void ibmvscsis_handle_crq(unsigned long data)
3157 struct scsi_info *vscsi = (struct scsi_info *)data;
3158 struct viosrp_crq *crq;
3163 spin_lock_bh(&vscsi->intr_lock);
3165 pr_debug("got interrupt\n");
3168 * if we are in a path where we are waiting for all pending commands
3169 * to complete because we received a transport event and anything in
3170 * the command queue is for a new connection, do nothing
3172 if (TARGET_STOP(vscsi)) {
3173 vio_enable_interrupts(vscsi->dma_dev);
3175 pr_debug("handle_crq, don't process: flags 0x%x, state 0x%hx\n",
3176 vscsi->flags, vscsi->state);
3177 spin_unlock_bh(&vscsi->intr_lock);
3181 rc = vscsi->flags & SCHEDULE_DISCONNECT;
3182 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
3188 * These are edege triggered interrupts. After dropping out of
3189 * the while loop, the code must check for work since an
3190 * interrupt could be lost, and an elment be left on the queue,
3194 vscsi->cmd_q.index =
3195 (vscsi->cmd_q.index + 1) & vscsi->cmd_q.mask;
3198 rc = ibmvscsis_parse_command(vscsi, crq);
3200 if ((uint)crq->valid == VALID_TRANS_EVENT) {
3202 * must service the transport layer events even
3203 * in an error state, dont break out until all
3204 * the consecutive transport events have been
3207 rc = ibmvscsis_trans_event(vscsi, crq);
3208 } else if (vscsi->flags & TRANS_EVENT) {
3210 * if a transport event has occurred leave
3211 * everything but transport events on the queue
3213 * need to decrement the queue index so we can
3214 * look at the element again
3216 if (vscsi->cmd_q.index)
3217 vscsi->cmd_q.index -= 1;
3220 * index is at 0 it just wrapped.
3221 * have it index last element in q
3223 vscsi->cmd_q.index = vscsi->cmd_q.mask;
3228 crq->valid = INVALIDATE_CMD_RESP_EL;
3230 crq = vscsi->cmd_q.base_addr + vscsi->cmd_q.index;
3237 vio_enable_interrupts(vscsi->dma_dev);
3239 pr_debug("handle_crq, reenabling interrupts\n");
3246 pr_debug("handle_crq, error: flags 0x%x, state 0x%hx, crq index 0x%x\n",
3247 vscsi->flags, vscsi->state, vscsi->cmd_q.index);
3250 pr_debug("Leaving handle_crq: schedule_q empty %d, flags 0x%x, state 0x%hx\n",
3251 (int)list_empty(&vscsi->schedule_q), vscsi->flags,
3254 spin_unlock_bh(&vscsi->intr_lock);
3257 static int ibmvscsis_probe(struct vio_dev *vdev,
3258 const struct vio_device_id *id)
3260 struct scsi_info *vscsi;
3265 vscsi = kzalloc(sizeof(*vscsi), GFP_KERNEL);
3268 pr_err("probe: allocation of adapter failed\n");
3272 vscsi->dma_dev = vdev;
3273 vscsi->dev = vdev->dev;
3274 INIT_LIST_HEAD(&vscsi->schedule_q);
3275 INIT_LIST_HEAD(&vscsi->waiting_rsp);
3276 INIT_LIST_HEAD(&vscsi->active_q);
3278 snprintf(vscsi->tport.tport_name, IBMVSCSIS_NAMELEN, "%s",
3279 dev_name(&vdev->dev));
3281 pr_debug("probe tport_name: %s\n", vscsi->tport.tport_name);
3283 rc = read_dma_window(vscsi);
3286 pr_debug("Probe: liobn 0x%x, riobn 0x%x\n",
3287 vscsi->dds.window[LOCAL].liobn,
3288 vscsi->dds.window[REMOTE].liobn);
3290 strcpy(vscsi->eye, "VSCSI ");
3291 strncat(vscsi->eye, vdev->name, MAX_EYE);
3293 vscsi->dds.unit_id = vdev->unit_address;
3294 strncpy(vscsi->dds.partition_name, partition_name,
3295 sizeof(vscsi->dds.partition_name));
3296 vscsi->dds.partition_num = partition_number;
3298 spin_lock_bh(&ibmvscsis_dev_lock);
3299 list_add_tail(&vscsi->list, &ibmvscsis_dev_list);
3300 spin_unlock_bh(&ibmvscsis_dev_lock);
3303 * TBD: How do we determine # of cmds to request? Do we know how
3304 * many "children" we have?
3306 vscsi->request_limit = INITIAL_SRP_LIMIT;
3307 rc = srp_target_alloc(&vscsi->target, &vdev->dev, vscsi->request_limit,
3312 vscsi->target.ldata = vscsi;
3314 rc = ibmvscsis_alloc_cmds(vscsi, vscsi->request_limit);
3316 dev_err(&vscsi->dev, "alloc_cmds failed, rc %d, num %d\n",
3317 rc, vscsi->request_limit);
3322 * Note: the lock is used in freeing timers, so must initialize
3323 * first so that ordering in case of error is correct.
3325 spin_lock_init(&vscsi->intr_lock);
3327 rc = ibmvscsis_alloctimer(vscsi);
3329 dev_err(&vscsi->dev, "probe: alloctimer failed, rc %d\n", rc);
3333 rc = ibmvscsis_create_command_q(vscsi, 256);
3335 dev_err(&vscsi->dev, "probe: create_command_q failed, rc %d\n",
3340 vscsi->map_buf = kzalloc(PAGE_SIZE, GFP_KERNEL);
3341 if (!vscsi->map_buf) {
3343 dev_err(&vscsi->dev, "probe: allocating cmd buffer failed\n");
3347 vscsi->map_ioba = dma_map_single(&vdev->dev, vscsi->map_buf, PAGE_SIZE,
3349 if (dma_mapping_error(&vdev->dev, vscsi->map_ioba)) {
3351 dev_err(&vscsi->dev, "probe: error mapping command buffer\n");
3355 hrc = h_vioctl(vscsi->dds.unit_id, H_GET_PARTNER_INFO,
3356 (u64)vscsi->map_ioba | ((u64)PAGE_SIZE << 32), 0, 0, 0,
3358 if (hrc == H_SUCCESS)
3359 vscsi->client_data.partition_number =
3360 be64_to_cpu(*(u64 *)vscsi->map_buf);
3362 * We expect the VIOCTL to fail if we're configured as "any
3363 * client can connect" and the client isn't activated yet.
3364 * We'll make the call again when he sends an init msg.
3366 pr_debug("probe hrc %ld, client partition num %d\n",
3367 hrc, vscsi->client_data.partition_number);
3369 tasklet_init(&vscsi->work_task, ibmvscsis_handle_crq,
3370 (unsigned long)vscsi);
3372 init_completion(&vscsi->wait_idle);
3373 init_completion(&vscsi->unconfig);
3375 snprintf(wq_name, 24, "ibmvscsis%s", dev_name(&vdev->dev));
3376 vscsi->work_q = create_workqueue(wq_name);
3377 if (!vscsi->work_q) {
3379 dev_err(&vscsi->dev, "create_workqueue failed\n");
3383 rc = request_irq(vdev->irq, ibmvscsis_interrupt, 0, "ibmvscsis", vscsi);
3386 dev_err(&vscsi->dev, "probe: request_irq failed, rc %d\n", rc);
3390 vscsi->state = WAIT_ENABLED;
3392 dev_set_drvdata(&vdev->dev, vscsi);
3397 destroy_workqueue(vscsi->work_q);
3399 dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
3402 kfree(vscsi->map_buf);
3404 tasklet_kill(&vscsi->work_task);
3405 ibmvscsis_unregister_command_q(vscsi);
3406 ibmvscsis_destroy_command_q(vscsi);
3408 ibmvscsis_freetimer(vscsi);
3410 ibmvscsis_free_cmds(vscsi);
3412 srp_target_free(&vscsi->target);
3414 spin_lock_bh(&ibmvscsis_dev_lock);
3415 list_del(&vscsi->list);
3416 spin_unlock_bh(&ibmvscsis_dev_lock);
3423 static int ibmvscsis_remove(struct vio_dev *vdev)
3425 struct scsi_info *vscsi = dev_get_drvdata(&vdev->dev);
3427 pr_debug("remove (%s)\n", dev_name(&vscsi->dma_dev->dev));
3429 spin_lock_bh(&vscsi->intr_lock);
3430 ibmvscsis_post_disconnect(vscsi, UNCONFIGURING, 0);
3431 vscsi->flags |= CFG_SLEEPING;
3432 spin_unlock_bh(&vscsi->intr_lock);
3433 wait_for_completion(&vscsi->unconfig);
3435 vio_disable_interrupts(vdev);
3436 free_irq(vdev->irq, vscsi);
3437 destroy_workqueue(vscsi->work_q);
3438 dma_unmap_single(&vdev->dev, vscsi->map_ioba, PAGE_SIZE,
3440 kfree(vscsi->map_buf);
3441 tasklet_kill(&vscsi->work_task);
3442 ibmvscsis_destroy_command_q(vscsi);
3443 ibmvscsis_freetimer(vscsi);
3444 ibmvscsis_free_cmds(vscsi);
3445 srp_target_free(&vscsi->target);
3446 spin_lock_bh(&ibmvscsis_dev_lock);
3447 list_del(&vscsi->list);
3448 spin_unlock_bh(&ibmvscsis_dev_lock);
3454 static ssize_t system_id_show(struct device *dev,
3455 struct device_attribute *attr, char *buf)
3457 return snprintf(buf, PAGE_SIZE, "%s\n", system_id);
3460 static ssize_t partition_number_show(struct device *dev,
3461 struct device_attribute *attr, char *buf)
3463 return snprintf(buf, PAGE_SIZE, "%x\n", partition_number);
3466 static ssize_t unit_address_show(struct device *dev,
3467 struct device_attribute *attr, char *buf)
3469 struct scsi_info *vscsi = container_of(dev, struct scsi_info, dev);
3471 return snprintf(buf, PAGE_SIZE, "%x\n", vscsi->dma_dev->unit_address);
3474 static int ibmvscsis_get_system_info(void)
3476 struct device_node *rootdn, *vdevdn;
3477 const char *id, *model, *name;
3480 rootdn = of_find_node_by_path("/");
3484 model = of_get_property(rootdn, "model", NULL);
3485 id = of_get_property(rootdn, "system-id", NULL);
3487 snprintf(system_id, sizeof(system_id), "%s-%s", model, id);
3489 name = of_get_property(rootdn, "ibm,partition-name", NULL);
3491 strncpy(partition_name, name, sizeof(partition_name));
3493 num = of_get_property(rootdn, "ibm,partition-no", NULL);
3495 partition_number = of_read_number(num, 1);
3497 of_node_put(rootdn);
3499 vdevdn = of_find_node_by_path("/vdevice");
3503 mvds = of_get_property(vdevdn, "ibm,max-virtual-dma-size",
3506 max_vdma_size = *mvds;
3507 of_node_put(vdevdn);
3513 static char *ibmvscsis_get_fabric_name(void)
3518 static char *ibmvscsis_get_fabric_wwn(struct se_portal_group *se_tpg)
3520 struct ibmvscsis_tport *tport =
3521 container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
3523 return tport->tport_name;
3526 static u16 ibmvscsis_get_tag(struct se_portal_group *se_tpg)
3528 struct ibmvscsis_tport *tport =
3529 container_of(se_tpg, struct ibmvscsis_tport, se_tpg);
3531 return tport->tport_tpgt;
3534 static u32 ibmvscsis_get_default_depth(struct se_portal_group *se_tpg)
3539 static int ibmvscsis_check_true(struct se_portal_group *se_tpg)
3544 static int ibmvscsis_check_false(struct se_portal_group *se_tpg)
3549 static u32 ibmvscsis_tpg_get_inst_index(struct se_portal_group *se_tpg)
3554 static int ibmvscsis_check_stop_free(struct se_cmd *se_cmd)
3556 return target_put_sess_cmd(se_cmd);
3559 static void ibmvscsis_release_cmd(struct se_cmd *se_cmd)
3561 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3563 struct scsi_info *vscsi = cmd->adapter;
3565 spin_lock_bh(&vscsi->intr_lock);
3566 /* Remove from active_q */
3567 list_move_tail(&cmd->list, &vscsi->waiting_rsp);
3568 ibmvscsis_send_messages(vscsi);
3569 spin_unlock_bh(&vscsi->intr_lock);
3572 static u32 ibmvscsis_sess_get_index(struct se_session *se_sess)
3577 static int ibmvscsis_write_pending(struct se_cmd *se_cmd)
3579 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3581 struct iu_entry *iue = cmd->iue;
3584 rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma,
3587 pr_err("srp_transfer_data() failed: %d\n", rc);
3591 * We now tell TCM to add this WRITE CDB directly into the TCM storage
3592 * object execution queue.
3594 target_execute_cmd(se_cmd);
3598 static int ibmvscsis_write_pending_status(struct se_cmd *se_cmd)
3603 static void ibmvscsis_set_default_node_attrs(struct se_node_acl *nacl)
3607 static int ibmvscsis_get_cmd_state(struct se_cmd *se_cmd)
3612 static int ibmvscsis_queue_data_in(struct se_cmd *se_cmd)
3614 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3616 struct iu_entry *iue = cmd->iue;
3617 struct scsi_info *vscsi = cmd->adapter;
3622 rc = srp_transfer_data(cmd, &vio_iu(iue)->srp.cmd, ibmvscsis_rdma, 1,
3625 pr_err("srp_transfer_data failed: %d\n", rc);
3626 sd = se_cmd->sense_buffer;
3627 se_cmd->scsi_sense_length = 18;
3628 memset(se_cmd->sense_buffer, 0, se_cmd->scsi_sense_length);
3629 /* Logical Unit Communication Time-out asc/ascq = 0x0801 */
3630 scsi_build_sense_buffer(0, se_cmd->sense_buffer, MEDIUM_ERROR,
3634 srp_build_response(vscsi, cmd, &len);
3635 cmd->rsp.format = SRP_FORMAT;
3641 static int ibmvscsis_queue_status(struct se_cmd *se_cmd)
3643 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3645 struct scsi_info *vscsi = cmd->adapter;
3648 pr_debug("queue_status %p\n", se_cmd);
3650 srp_build_response(vscsi, cmd, &len);
3651 cmd->rsp.format = SRP_FORMAT;
3657 static void ibmvscsis_queue_tm_rsp(struct se_cmd *se_cmd)
3659 struct ibmvscsis_cmd *cmd = container_of(se_cmd, struct ibmvscsis_cmd,
3661 struct scsi_info *vscsi = cmd->adapter;
3664 pr_debug("queue_tm_rsp %p, status %d\n",
3665 se_cmd, (int)se_cmd->se_tmr_req->response);
3667 srp_build_response(vscsi, cmd, &len);
3668 cmd->rsp.format = SRP_FORMAT;
3672 static void ibmvscsis_aborted_task(struct se_cmd *se_cmd)
3674 /* TBD: What (if anything) should we do here? */
3675 pr_debug("ibmvscsis_aborted_task %p\n", se_cmd);
3678 static struct se_wwn *ibmvscsis_make_tport(struct target_fabric_configfs *tf,
3679 struct config_group *group,
3682 struct ibmvscsis_tport *tport;
3684 tport = ibmvscsis_lookup_port(name);
3686 tport->tport_proto_id = SCSI_PROTOCOL_SRP;
3687 pr_debug("make_tport(%s), pointer:%p, tport_id:%x\n",
3688 name, tport, tport->tport_proto_id);
3689 return &tport->tport_wwn;
3692 return ERR_PTR(-EINVAL);
3695 static void ibmvscsis_drop_tport(struct se_wwn *wwn)
3697 struct ibmvscsis_tport *tport = container_of(wwn,
3698 struct ibmvscsis_tport,
3701 pr_debug("drop_tport(%s)\n",
3702 config_item_name(&tport->tport_wwn.wwn_group.cg_item));
3705 static struct se_portal_group *ibmvscsis_make_tpg(struct se_wwn *wwn,
3706 struct config_group *group,
3709 struct ibmvscsis_tport *tport =
3710 container_of(wwn, struct ibmvscsis_tport, tport_wwn);
3713 tport->releasing = false;
3715 rc = core_tpg_register(&tport->tport_wwn, &tport->se_tpg,
3716 tport->tport_proto_id);
3720 return &tport->se_tpg;
3723 static void ibmvscsis_drop_tpg(struct se_portal_group *se_tpg)
3725 struct ibmvscsis_tport *tport = container_of(se_tpg,
3726 struct ibmvscsis_tport,
3729 tport->releasing = true;
3730 tport->enabled = false;
3733 * Release the virtual I_T Nexus for this ibmvscsis TPG
3735 ibmvscsis_drop_nexus(tport);
3737 * Deregister the se_tpg from TCM..
3739 core_tpg_deregister(se_tpg);
3742 static ssize_t ibmvscsis_wwn_version_show(struct config_item *item,
3745 return scnprintf(page, PAGE_SIZE, "%s\n", IBMVSCSIS_VERSION);
3747 CONFIGFS_ATTR_RO(ibmvscsis_wwn_, version);
3749 static struct configfs_attribute *ibmvscsis_wwn_attrs[] = {
3750 &ibmvscsis_wwn_attr_version,
3754 static ssize_t ibmvscsis_tpg_enable_show(struct config_item *item,
3757 struct se_portal_group *se_tpg = to_tpg(item);
3758 struct ibmvscsis_tport *tport = container_of(se_tpg,
3759 struct ibmvscsis_tport,
3762 return snprintf(page, PAGE_SIZE, "%d\n", (tport->enabled) ? 1 : 0);
3765 static ssize_t ibmvscsis_tpg_enable_store(struct config_item *item,
3766 const char *page, size_t count)
3768 struct se_portal_group *se_tpg = to_tpg(item);
3769 struct ibmvscsis_tport *tport = container_of(se_tpg,
3770 struct ibmvscsis_tport,
3772 struct scsi_info *vscsi = container_of(tport, struct scsi_info, tport);
3777 rc = kstrtoul(page, 0, &tmp);
3779 pr_err("Unable to extract srpt_tpg_store_enable\n");
3783 if ((tmp != 0) && (tmp != 1)) {
3784 pr_err("Illegal value for srpt_tpg_store_enable\n");
3789 spin_lock_bh(&vscsi->intr_lock);
3790 tport->enabled = true;
3791 lrc = ibmvscsis_enable_change_state(vscsi);
3793 pr_err("enable_change_state failed, rc %ld state %d\n",
3795 spin_unlock_bh(&vscsi->intr_lock);
3797 spin_lock_bh(&vscsi->intr_lock);
3798 tport->enabled = false;
3799 /* This simulates the server going down */
3800 ibmvscsis_post_disconnect(vscsi, ERR_DISCONNECT, 0);
3801 spin_unlock_bh(&vscsi->intr_lock);
3804 pr_debug("tpg_enable_store, tmp %ld, state %d\n", tmp, vscsi->state);
3808 CONFIGFS_ATTR(ibmvscsis_tpg_, enable);
3810 static struct configfs_attribute *ibmvscsis_tpg_attrs[] = {
3811 &ibmvscsis_tpg_attr_enable,
3815 static const struct target_core_fabric_ops ibmvscsis_ops = {
3816 .module = THIS_MODULE,
3817 .name = "ibmvscsis",
3818 .get_fabric_name = ibmvscsis_get_fabric_name,
3819 .tpg_get_wwn = ibmvscsis_get_fabric_wwn,
3820 .tpg_get_tag = ibmvscsis_get_tag,
3821 .tpg_get_default_depth = ibmvscsis_get_default_depth,
3822 .tpg_check_demo_mode = ibmvscsis_check_true,
3823 .tpg_check_demo_mode_cache = ibmvscsis_check_true,
3824 .tpg_check_demo_mode_write_protect = ibmvscsis_check_false,
3825 .tpg_check_prod_mode_write_protect = ibmvscsis_check_false,
3826 .tpg_get_inst_index = ibmvscsis_tpg_get_inst_index,
3827 .check_stop_free = ibmvscsis_check_stop_free,
3828 .release_cmd = ibmvscsis_release_cmd,
3829 .sess_get_index = ibmvscsis_sess_get_index,
3830 .write_pending = ibmvscsis_write_pending,
3831 .write_pending_status = ibmvscsis_write_pending_status,
3832 .set_default_node_attributes = ibmvscsis_set_default_node_attrs,
3833 .get_cmd_state = ibmvscsis_get_cmd_state,
3834 .queue_data_in = ibmvscsis_queue_data_in,
3835 .queue_status = ibmvscsis_queue_status,
3836 .queue_tm_rsp = ibmvscsis_queue_tm_rsp,
3837 .aborted_task = ibmvscsis_aborted_task,
3839 * Setup function pointers for logic in target_core_fabric_configfs.c
3841 .fabric_make_wwn = ibmvscsis_make_tport,
3842 .fabric_drop_wwn = ibmvscsis_drop_tport,
3843 .fabric_make_tpg = ibmvscsis_make_tpg,
3844 .fabric_drop_tpg = ibmvscsis_drop_tpg,
3846 .tfc_wwn_attrs = ibmvscsis_wwn_attrs,
3847 .tfc_tpg_base_attrs = ibmvscsis_tpg_attrs,
3850 static void ibmvscsis_dev_release(struct device *dev) {};
3852 static struct class_attribute ibmvscsis_class_attrs[] = {
3856 static struct device_attribute dev_attr_system_id =
3857 __ATTR(system_id, S_IRUGO, system_id_show, NULL);
3859 static struct device_attribute dev_attr_partition_number =
3860 __ATTR(partition_number, S_IRUGO, partition_number_show, NULL);
3862 static struct device_attribute dev_attr_unit_address =
3863 __ATTR(unit_address, S_IRUGO, unit_address_show, NULL);
3865 static struct attribute *ibmvscsis_dev_attrs[] = {
3866 &dev_attr_system_id.attr,
3867 &dev_attr_partition_number.attr,
3868 &dev_attr_unit_address.attr,
3870 ATTRIBUTE_GROUPS(ibmvscsis_dev);
3872 static struct class ibmvscsis_class = {
3873 .name = "ibmvscsis",
3874 .dev_release = ibmvscsis_dev_release,
3875 .class_attrs = ibmvscsis_class_attrs,
3876 .dev_groups = ibmvscsis_dev_groups,
3879 static struct vio_device_id ibmvscsis_device_table[] = {
3880 { "v-scsi-host", "IBM,v-scsi-host" },
3883 MODULE_DEVICE_TABLE(vio, ibmvscsis_device_table);
3885 static struct vio_driver ibmvscsis_driver = {
3886 .name = "ibmvscsis",
3887 .id_table = ibmvscsis_device_table,
3888 .probe = ibmvscsis_probe,
3889 .remove = ibmvscsis_remove,
3893 * ibmvscsis_init() - Kernel Module initialization
3895 * Note: vio_register_driver() registers callback functions, and at least one
3896 * of those callback functions calls TCM - Linux IO Target Subsystem, thus
3897 * the SCSI Target template must be registered before vio_register_driver()
3900 static int __init ibmvscsis_init(void)
3904 rc = ibmvscsis_get_system_info();
3906 pr_err("rc %d from get_system_info\n", rc);
3910 rc = class_register(&ibmvscsis_class);
3912 pr_err("failed class register\n");
3916 rc = target_register_template(&ibmvscsis_ops);
3918 pr_err("rc %d from target_register_template\n", rc);
3919 goto unregister_class;
3922 rc = vio_register_driver(&ibmvscsis_driver);
3924 pr_err("rc %d from vio_register_driver\n", rc);
3925 goto unregister_target;
3931 target_unregister_template(&ibmvscsis_ops);
3933 class_unregister(&ibmvscsis_class);
3938 static void __exit ibmvscsis_exit(void)
3940 pr_info("Unregister IBM virtual SCSI host driver\n");
3941 vio_unregister_driver(&ibmvscsis_driver);
3942 target_unregister_template(&ibmvscsis_ops);
3943 class_unregister(&ibmvscsis_class);
3946 MODULE_DESCRIPTION("IBMVSCSIS fabric driver");
3947 MODULE_AUTHOR("Bryant G. Ly and Michael Cyr");
3948 MODULE_LICENSE("GPL");
3949 MODULE_VERSION(IBMVSCSIS_VERSION);
3950 module_init(ibmvscsis_init);
3951 module_exit(ibmvscsis_exit);