1 /*******************************************************************************
2 * IBM Virtual SCSI Target Driver
3 * Copyright (C) 2003-2005 Dave Boutcher (boutcher@us.ibm.com) IBM Corp.
4 * Santiago Leon (santil@us.ibm.com) IBM Corp.
5 * Linda Xie (lxie@us.ibm.com) IBM Corp.
7 * Copyright (C) 2005-2011 FUJITA Tomonori <tomof@acm.org>
8 * Copyright (C) 2010 Nicholas A. Bellinger <nab@kernel.org>
10 * Authors: Bryant G. Ly <bryantly@linux.vnet.ibm.com>
11 * Authors: Michael Cyr <mikecyr@linux.vnet.ibm.com>
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 ****************************************************************************/
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
27 #include <linux/module.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/types.h>
31 #include <linux/list.h>
32 #include <linux/string.h>
34 #include <target/target_core_base.h>
35 #include <target/target_core_fabric.h>
37 #include <asm/hvcall.h>
40 #include <scsi/viosrp.h>
42 #include "ibmvscsi_tgt.h"
44 #define IBMVSCSIS_VERSION "v0.2"
46 #define INITIAL_SRP_LIMIT 800
47 #define DEFAULT_MAX_SECTORS 256
49 static uint max_vdma_size
= MAX_H_COPY_RDMA
;
51 static char system_id
[SYS_ID_NAME_LEN
] = "";
52 static char partition_name
[PARTITION_NAMELEN
] = "UNKNOWN";
53 static uint partition_number
= -1;
55 /* Adapter list and lock to control it */
56 static DEFINE_SPINLOCK(ibmvscsis_dev_lock
);
57 static LIST_HEAD(ibmvscsis_dev_list
);
59 static long ibmvscsis_parse_command(struct scsi_info
*vscsi
,
60 struct viosrp_crq
*crq
);
62 static void ibmvscsis_adapter_idle(struct scsi_info
*vscsi
);
64 static void ibmvscsis_determine_resid(struct se_cmd
*se_cmd
,
67 u32 residual_count
= se_cmd
->residual_count
;
72 if (se_cmd
->se_cmd_flags
& SCF_UNDERFLOW_BIT
) {
73 if (se_cmd
->data_direction
== DMA_TO_DEVICE
) {
74 /* residual data from an underflow write */
75 rsp
->flags
= SRP_RSP_FLAG_DOUNDER
;
76 rsp
->data_out_res_cnt
= cpu_to_be32(residual_count
);
77 } else if (se_cmd
->data_direction
== DMA_FROM_DEVICE
) {
78 /* residual data from an underflow read */
79 rsp
->flags
= SRP_RSP_FLAG_DIUNDER
;
80 rsp
->data_in_res_cnt
= cpu_to_be32(residual_count
);
82 } else if (se_cmd
->se_cmd_flags
& SCF_OVERFLOW_BIT
) {
83 if (se_cmd
->data_direction
== DMA_TO_DEVICE
) {
84 /* residual data from an overflow write */
85 rsp
->flags
= SRP_RSP_FLAG_DOOVER
;
86 rsp
->data_out_res_cnt
= cpu_to_be32(residual_count
);
87 } else if (se_cmd
->data_direction
== DMA_FROM_DEVICE
) {
88 /* residual data from an overflow read */
89 rsp
->flags
= SRP_RSP_FLAG_DIOVER
;
90 rsp
->data_in_res_cnt
= cpu_to_be32(residual_count
);
96 * connection_broken() - Determine if the connection to the client is good
97 * @vscsi: Pointer to our adapter structure
99 * This function attempts to send a ping MAD to the client. If the call to
100 * queue the request returns H_CLOSED then the connection has been broken
101 * and the function returns TRUE.
103 * EXECUTION ENVIRONMENT:
104 * Interrupt or Process environment
106 static bool connection_broken(struct scsi_info
*vscsi
)
108 struct viosrp_crq
*crq
;
109 u64 buffer
[2] = { 0, 0 };
113 /* create a PING crq */
114 crq
= (struct viosrp_crq
*)&buffer
;
115 crq
->valid
= VALID_CMD_RESP_EL
;
116 crq
->format
= MESSAGE_IN_CRQ
;
119 h_return_code
= h_send_crq(vscsi
->dds
.unit_id
,
120 cpu_to_be64(buffer
[MSG_HI
]),
121 cpu_to_be64(buffer
[MSG_LOW
]));
123 pr_debug("connection_broken: rc %ld\n", h_return_code
);
125 if (h_return_code
== H_CLOSED
)
132 * ibmvscsis_unregister_command_q() - Helper Function-Unregister Command Queue
133 * @vscsi: Pointer to our adapter structure
135 * This function calls h_free_q then frees the interrupt bit etc.
136 * It must release the lock before doing so because of the time it can take
137 * for h_free_crq in PHYP
138 * NOTE: the caller must make sure that state and or flags will prevent
139 * interrupt handler from scheduling work.
140 * NOTE: anyone calling this function may need to set the CRQ_CLOSED flag
141 * we can't do it here, because we don't have the lock
143 * EXECUTION ENVIRONMENT:
146 static long ibmvscsis_unregister_command_q(struct scsi_info
*vscsi
)
149 long rc
= ADAPT_SUCCESS
;
153 qrc
= h_free_crq(vscsi
->dds
.unit_id
);
160 dev_err(&vscsi
->dev
, "unregister_command_q: error from h_free_crq %ld\n",
166 case H_LONG_BUSY_ORDER_1_MSEC
:
167 /* msleep not good for small values */
168 usleep_range(1000, 2000);
171 case H_LONG_BUSY_ORDER_10_MSEC
:
172 usleep_range(10000, 20000);
175 case H_LONG_BUSY_ORDER_100_MSEC
:
179 case H_LONG_BUSY_ORDER_1_SEC
:
183 case H_LONG_BUSY_ORDER_10_SEC
:
187 case H_LONG_BUSY_ORDER_100_SEC
:
192 dev_err(&vscsi
->dev
, "unregister_command_q: unknown error %ld from h_free_crq\n",
199 * dont wait more then 300 seconds
200 * ticks are in milliseconds more or less
202 if (ticks
> 300000 && qrc
!= H_SUCCESS
) {
204 dev_err(&vscsi
->dev
, "Excessive wait for h_free_crq\n");
206 } while (qrc
!= H_SUCCESS
&& rc
== ADAPT_SUCCESS
);
208 pr_debug("Freeing CRQ: phyp rc %ld, rc %ld\n", qrc
, rc
);
214 * ibmvscsis_delete_client_info() - Helper function to Delete Client Info
215 * @vscsi: Pointer to our adapter structure
216 * @client_closed: True if client closed its queue
218 * Deletes information specific to the client when the client goes away
220 * EXECUTION ENVIRONMENT:
221 * Interrupt or Process
223 static void ibmvscsis_delete_client_info(struct scsi_info
*vscsi
,
226 vscsi
->client_cap
= 0;
229 * Some things we don't want to clear if we're closing the queue,
230 * because some clients don't resend the host handshake when they
231 * get a transport event.
234 vscsi
->client_data
.os_type
= 0;
238 * ibmvscsis_free_command_q() - Free Command Queue
239 * @vscsi: Pointer to our adapter structure
241 * This function calls unregister_command_q, then clears interrupts and
242 * any pending interrupt acknowledgments associated with the command q.
243 * It also clears memory if there is no error.
245 * PHYP did not meet the PAPR architecture so that we must give up the
246 * lock. This causes a timing hole regarding state change. To close the
247 * hole this routine does accounting on any change that occurred during
248 * the time the lock is not held.
249 * NOTE: must give up and then acquire the interrupt lock, the caller must
250 * make sure that state and or flags will prevent interrupt handler from
253 * EXECUTION ENVIRONMENT:
254 * Process level, interrupt lock is held
256 static long ibmvscsis_free_command_q(struct scsi_info
*vscsi
)
259 u32 flags_under_lock
;
260 u16 state_under_lock
;
261 long rc
= ADAPT_SUCCESS
;
263 if (!(vscsi
->flags
& CRQ_CLOSED
)) {
264 vio_disable_interrupts(vscsi
->dma_dev
);
266 state_under_lock
= vscsi
->new_state
;
267 flags_under_lock
= vscsi
->flags
;
268 vscsi
->phyp_acr_state
= 0;
269 vscsi
->phyp_acr_flags
= 0;
271 spin_unlock_bh(&vscsi
->intr_lock
);
272 rc
= ibmvscsis_unregister_command_q(vscsi
);
273 spin_lock_bh(&vscsi
->intr_lock
);
275 if (state_under_lock
!= vscsi
->new_state
)
276 vscsi
->phyp_acr_state
= vscsi
->new_state
;
278 vscsi
->phyp_acr_flags
= ((~flags_under_lock
) & vscsi
->flags
);
280 if (rc
== ADAPT_SUCCESS
) {
281 bytes
= vscsi
->cmd_q
.size
* PAGE_SIZE
;
282 memset(vscsi
->cmd_q
.base_addr
, 0, bytes
);
283 vscsi
->cmd_q
.index
= 0;
284 vscsi
->flags
|= CRQ_CLOSED
;
286 ibmvscsis_delete_client_info(vscsi
, false);
289 pr_debug("free_command_q: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
290 vscsi
->flags
, vscsi
->state
, vscsi
->phyp_acr_flags
,
291 vscsi
->phyp_acr_state
);
297 * ibmvscsis_cmd_q_dequeue() - Get valid Command element
298 * @mask: Mask to use in case index wraps
299 * @current_index: Current index into command queue
300 * @base_addr: Pointer to start of command queue
302 * Returns a pointer to a valid command element or NULL, if the command
305 * EXECUTION ENVIRONMENT:
306 * Interrupt environment, interrupt lock held
308 static struct viosrp_crq
*ibmvscsis_cmd_q_dequeue(uint mask
,
310 struct viosrp_crq
*base_addr
)
312 struct viosrp_crq
*ptr
;
314 ptr
= base_addr
+ *current_index
;
317 *current_index
= (*current_index
+ 1) & mask
;
327 * ibmvscsis_send_init_message() - send initialize message to the client
328 * @vscsi: Pointer to our adapter structure
329 * @format: Which Init Message format to send
331 * EXECUTION ENVIRONMENT:
332 * Interrupt environment interrupt lock held
334 static long ibmvscsis_send_init_message(struct scsi_info
*vscsi
, u8 format
)
336 struct viosrp_crq
*crq
;
337 u64 buffer
[2] = { 0, 0 };
340 crq
= (struct viosrp_crq
*)&buffer
;
341 crq
->valid
= VALID_INIT_MSG
;
342 crq
->format
= format
;
343 rc
= h_send_crq(vscsi
->dds
.unit_id
, cpu_to_be64(buffer
[MSG_HI
]),
344 cpu_to_be64(buffer
[MSG_LOW
]));
350 * ibmvscsis_check_init_msg() - Check init message valid
351 * @vscsi: Pointer to our adapter structure
352 * @format: Pointer to return format of Init Message, if any.
353 * Set to UNUSED_FORMAT if no Init Message in queue.
355 * Checks if an initialize message was queued by the initiatior
356 * after the queue was created and before the interrupt was enabled.
358 * EXECUTION ENVIRONMENT:
359 * Process level only, interrupt lock held
361 static long ibmvscsis_check_init_msg(struct scsi_info
*vscsi
, uint
*format
)
363 struct viosrp_crq
*crq
;
364 long rc
= ADAPT_SUCCESS
;
366 crq
= ibmvscsis_cmd_q_dequeue(vscsi
->cmd_q
.mask
, &vscsi
->cmd_q
.index
,
367 vscsi
->cmd_q
.base_addr
);
369 *format
= (uint
)UNUSED_FORMAT
;
370 } else if (crq
->valid
== VALID_INIT_MSG
&& crq
->format
== INIT_MSG
) {
371 *format
= (uint
)INIT_MSG
;
372 crq
->valid
= INVALIDATE_CMD_RESP_EL
;
376 * the caller has ensured no initialize message was
377 * sent after the queue was
378 * created so there should be no other message on the queue.
380 crq
= ibmvscsis_cmd_q_dequeue(vscsi
->cmd_q
.mask
,
382 vscsi
->cmd_q
.base_addr
);
384 *format
= (uint
)(crq
->format
);
386 crq
->valid
= INVALIDATE_CMD_RESP_EL
;
390 *format
= (uint
)(crq
->format
);
392 crq
->valid
= INVALIDATE_CMD_RESP_EL
;
400 * ibmvscsis_establish_new_q() - Establish new CRQ queue
401 * @vscsi: Pointer to our adapter structure
402 * @new_state: New state being established after resetting the queue
404 * Must be called with interrupt lock held.
406 static long ibmvscsis_establish_new_q(struct scsi_info
*vscsi
, uint new_state
)
408 long rc
= ADAPT_SUCCESS
;
411 vscsi
->flags
&= PRESERVE_FLAG_FIELDS
;
412 vscsi
->rsp_q_timer
.timer_pops
= 0;
416 rc
= vio_enable_interrupts(vscsi
->dma_dev
);
418 pr_warn("reset_queue: failed to enable interrupts, rc %ld\n",
423 rc
= ibmvscsis_check_init_msg(vscsi
, &format
);
425 dev_err(&vscsi
->dev
, "reset_queue: check_init_msg failed, rc %ld\n",
430 if (format
== UNUSED_FORMAT
&& new_state
== WAIT_CONNECTION
) {
431 rc
= ibmvscsis_send_init_message(vscsi
, INIT_MSG
);
444 vscsi
->state
= UNDEFINED
;
454 * ibmvscsis_reset_queue() - Reset CRQ Queue
455 * @vscsi: Pointer to our adapter structure
456 * @new_state: New state to establish after resetting the queue
458 * This function calls h_free_q and then calls h_reg_q and does all
459 * of the bookkeeping to get us back to where we can communicate.
461 * Actually, we don't always call h_free_crq. A problem was discovered
462 * where one partition would close and reopen his queue, which would
463 * cause his partner to get a transport event, which would cause him to
464 * close and reopen his queue, which would cause the original partition
465 * to get a transport event, etc., etc. To prevent this, we don't
466 * actually close our queue if the client initiated the reset, (i.e.
467 * either we got a transport event or we have detected that the client's
470 * EXECUTION ENVIRONMENT:
471 * Process environment, called with interrupt lock held
473 static void ibmvscsis_reset_queue(struct scsi_info
*vscsi
, uint new_state
)
476 long rc
= ADAPT_SUCCESS
;
478 pr_debug("reset_queue: flags 0x%x\n", vscsi
->flags
);
480 /* don't reset, the client did it for us */
481 if (vscsi
->flags
& (CLIENT_FAILED
| TRANS_EVENT
)) {
482 vscsi
->flags
&= PRESERVE_FLAG_FIELDS
;
483 vscsi
->rsp_q_timer
.timer_pops
= 0;
486 vscsi
->state
= new_state
;
487 vio_enable_interrupts(vscsi
->dma_dev
);
489 rc
= ibmvscsis_free_command_q(vscsi
);
490 if (rc
== ADAPT_SUCCESS
) {
491 vscsi
->state
= new_state
;
493 bytes
= vscsi
->cmd_q
.size
* PAGE_SIZE
;
494 rc
= h_reg_crq(vscsi
->dds
.unit_id
,
495 vscsi
->cmd_q
.crq_token
, bytes
);
496 if (rc
== H_CLOSED
|| rc
== H_SUCCESS
) {
497 rc
= ibmvscsis_establish_new_q(vscsi
,
501 if (rc
!= ADAPT_SUCCESS
) {
502 pr_debug("reset_queue: reg_crq rc %ld\n", rc
);
504 vscsi
->state
= ERR_DISCONNECTED
;
505 vscsi
->flags
|= RESPONSE_Q_DOWN
;
506 ibmvscsis_free_command_q(vscsi
);
509 vscsi
->state
= ERR_DISCONNECTED
;
510 vscsi
->flags
|= RESPONSE_Q_DOWN
;
516 * ibmvscsis_free_cmd_resources() - Free command resources
517 * @vscsi: Pointer to our adapter structure
518 * @cmd: Command which is not longer in use
520 * Must be called with interrupt lock held.
522 static void ibmvscsis_free_cmd_resources(struct scsi_info
*vscsi
,
523 struct ibmvscsis_cmd
*cmd
)
525 struct iu_entry
*iue
= cmd
->iue
;
528 case TASK_MANAGEMENT
:
531 * When the queue goes down this value is cleared, so it
532 * cannot be cleared in this general purpose function.
538 vscsi
->flags
&= ~PROCESSING_MAD
;
543 dev_err(&vscsi
->dev
, "free_cmd_resources unknown type %d\n",
549 list_add_tail(&cmd
->list
, &vscsi
->free_cmd
);
552 if (list_empty(&vscsi
->active_q
) && list_empty(&vscsi
->schedule_q
) &&
553 list_empty(&vscsi
->waiting_rsp
) && (vscsi
->flags
& WAIT_FOR_IDLE
)) {
554 vscsi
->flags
&= ~WAIT_FOR_IDLE
;
555 complete(&vscsi
->wait_idle
);
560 * ibmvscsis_disconnect() - Helper function to disconnect
561 * @work: Pointer to work_struct, gives access to our adapter structure
563 * An error has occurred or the driver received a Transport event,
564 * and the driver is requesting that the command queue be de-registered
565 * in a safe manner. If there is no outstanding I/O then we can stop the
566 * queue. If we are restarting the queue it will be reflected in the
567 * the state of the adapter.
569 * EXECUTION ENVIRONMENT:
570 * Process environment
572 static void ibmvscsis_disconnect(struct work_struct
*work
)
574 struct scsi_info
*vscsi
= container_of(work
, struct scsi_info
,
577 bool wait_idle
= false;
578 long rc
= ADAPT_SUCCESS
;
580 spin_lock_bh(&vscsi
->intr_lock
);
581 new_state
= vscsi
->new_state
;
582 vscsi
->new_state
= 0;
584 pr_debug("disconnect: flags 0x%x, state 0x%hx\n", vscsi
->flags
,
588 * check which state we are in and see if we
589 * should transitition to the new state
591 switch (vscsi
->state
) {
592 /* Should never be called while in this state. */
595 * Can never transition from this state;
596 * igonore errors and logout.
601 /* can transition from this state to UNCONFIGURING */
603 if (new_state
== UNCONFIGURING
)
604 vscsi
->state
= new_state
;
608 * Can transition from this state to to unconfiguring
611 case ERR_DISCONNECT_RECONNECT
:
615 vscsi
->state
= new_state
;
625 /* can transition from this state to UNCONFIGURING */
626 case ERR_DISCONNECTED
:
627 if (new_state
== UNCONFIGURING
)
628 vscsi
->state
= new_state
;
632 * If this is a transition into an error state.
633 * a client is attempting to establish a connection
634 * and has violated the RPA protocol.
635 * There can be nothing pending on the adapter although
636 * there can be requests in the command queue.
639 case PART_UP_WAIT_ENAB
:
642 vscsi
->flags
|= RESPONSE_Q_DOWN
;
643 vscsi
->state
= new_state
;
644 vscsi
->flags
&= ~(SCHEDULE_DISCONNECT
|
645 DISCONNECT_SCHEDULED
);
646 ibmvscsis_free_command_q(vscsi
);
648 case ERR_DISCONNECT_RECONNECT
:
649 ibmvscsis_reset_queue(vscsi
, WAIT_ENABLED
);
652 /* should never happen */
655 dev_err(&vscsi
->dev
, "disconnect: invalid state %d for WAIT_IDLE\n",
664 case ERR_DISCONNECT_RECONNECT
:
665 vscsi
->state
= new_state
;
671 * Initiator has not done a successful srp login
672 * or has done a successful srp logout ( adapter was not
673 * busy). In the first case there can be responses queued
674 * waiting for space on the initiators response queue (MAD)
675 * The second case the adapter is idle. Assume the worse case,
676 * i.e. the second case.
678 case WAIT_CONNECTION
:
682 vscsi
->state
= new_state
;
685 /* can transition from this state to UNCONFIGURING */
687 if (new_state
== UNCONFIGURING
)
688 vscsi
->state
= new_state
;
695 pr_debug("disconnect start wait, active %d, sched %d\n",
696 (int)list_empty(&vscsi
->active_q
),
697 (int)list_empty(&vscsi
->schedule_q
));
698 if (!list_empty(&vscsi
->active_q
) ||
699 !list_empty(&vscsi
->schedule_q
)) {
700 vscsi
->flags
|= WAIT_FOR_IDLE
;
701 pr_debug("disconnect flags 0x%x\n", vscsi
->flags
);
703 * This routine is can not be called with the interrupt
706 spin_unlock_bh(&vscsi
->intr_lock
);
707 wait_for_completion(&vscsi
->wait_idle
);
708 spin_lock_bh(&vscsi
->intr_lock
);
710 pr_debug("disconnect stop wait\n");
712 ibmvscsis_adapter_idle(vscsi
);
715 spin_unlock_bh(&vscsi
->intr_lock
);
719 * ibmvscsis_post_disconnect() - Schedule the disconnect
720 * @vscsi: Pointer to our adapter structure
721 * @new_state: State to move to after disconnecting
722 * @flag_bits: Flags to turn on in adapter structure
724 * If it's already been scheduled, then see if we need to "upgrade"
725 * the new state (if the one passed in is more "severe" than the
729 * interrupt lock is held
731 static void ibmvscsis_post_disconnect(struct scsi_info
*vscsi
, uint new_state
,
736 /* check the validity of the new state */
740 case ERR_DISCONNECT_RECONNECT
:
745 dev_err(&vscsi
->dev
, "post_disconnect: Invalid new state %d\n",
750 vscsi
->flags
|= flag_bits
;
752 pr_debug("post_disconnect: new_state 0x%x, flag_bits 0x%x, vscsi->flags 0x%x, state %hx\n",
753 new_state
, flag_bits
, vscsi
->flags
, vscsi
->state
);
755 if (!(vscsi
->flags
& (DISCONNECT_SCHEDULED
| SCHEDULE_DISCONNECT
))) {
756 vscsi
->flags
|= SCHEDULE_DISCONNECT
;
757 vscsi
->new_state
= new_state
;
759 INIT_WORK(&vscsi
->proc_work
, ibmvscsis_disconnect
);
760 (void)queue_work(vscsi
->work_q
, &vscsi
->proc_work
);
762 if (vscsi
->new_state
)
763 state
= vscsi
->new_state
;
765 state
= vscsi
->state
;
772 case ERR_DISCONNECTED
:
775 if (new_state
== UNCONFIGURING
)
776 vscsi
->new_state
= new_state
;
779 case ERR_DISCONNECT_RECONNECT
:
783 vscsi
->new_state
= new_state
;
791 case PART_UP_WAIT_ENAB
:
793 case WAIT_CONNECTION
:
796 vscsi
->new_state
= new_state
;
804 pr_debug("Leaving post_disconnect: flags 0x%x, new_state 0x%x\n",
805 vscsi
->flags
, vscsi
->new_state
);
809 * ibmvscsis_trans_event() - Handle a Transport Event
810 * @vscsi: Pointer to our adapter structure
811 * @crq: Pointer to CRQ entry containing the Transport Event
813 * Do the logic to close the I_T nexus. This function may not
814 * behave to specification.
816 * EXECUTION ENVIRONMENT:
817 * Interrupt, interrupt lock held
819 static long ibmvscsis_trans_event(struct scsi_info
*vscsi
,
820 struct viosrp_crq
*crq
)
822 long rc
= ADAPT_SUCCESS
;
824 pr_debug("trans_event: format %d, flags 0x%x, state 0x%hx\n",
825 (int)crq
->format
, vscsi
->flags
, vscsi
->state
);
827 switch (crq
->format
) {
830 case PARTNER_DEREGISTER
:
831 ibmvscsis_delete_client_info(vscsi
, true);
836 dev_err(&vscsi
->dev
, "trans_event: invalid format %d\n",
838 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT
,
843 if (rc
== ADAPT_SUCCESS
) {
844 switch (vscsi
->state
) {
846 case ERR_DISCONNECTED
:
851 vscsi
->flags
|= (RESPONSE_Q_DOWN
| TRANS_EVENT
);
857 case WAIT_CONNECTION
:
861 ibmvscsis_post_disconnect(vscsi
, WAIT_IDLE
,
866 case PART_UP_WAIT_ENAB
:
867 vscsi
->state
= WAIT_ENABLED
;
871 if ((vscsi
->debit
> 0) ||
872 !list_empty(&vscsi
->schedule_q
) ||
873 !list_empty(&vscsi
->waiting_rsp
) ||
874 !list_empty(&vscsi
->active_q
)) {
875 pr_debug("debit %d, sched %d, wait %d, active %d\n",
877 (int)list_empty(&vscsi
->schedule_q
),
878 (int)list_empty(&vscsi
->waiting_rsp
),
879 (int)list_empty(&vscsi
->active_q
));
880 pr_warn("connection lost with outstanding work\n");
882 pr_debug("trans_event: SRP Processing, but no outstanding work\n");
885 ibmvscsis_post_disconnect(vscsi
, WAIT_IDLE
,
891 case ERR_DISCONNECT_RECONNECT
:
893 vscsi
->flags
|= (RESPONSE_Q_DOWN
| TRANS_EVENT
);
898 rc
= vscsi
->flags
& SCHEDULE_DISCONNECT
;
900 pr_debug("Leaving trans_event: flags 0x%x, state 0x%hx, rc %ld\n",
901 vscsi
->flags
, vscsi
->state
, rc
);
907 * ibmvscsis_poll_cmd_q() - Poll Command Queue
908 * @vscsi: Pointer to our adapter structure
910 * Called to handle command elements that may have arrived while
911 * interrupts were disabled.
913 * EXECUTION ENVIRONMENT:
914 * intr_lock must be held
916 static void ibmvscsis_poll_cmd_q(struct scsi_info
*vscsi
)
918 struct viosrp_crq
*crq
;
923 pr_debug("poll_cmd_q: flags 0x%x, state 0x%hx, q index %ud\n",
924 vscsi
->flags
, vscsi
->state
, vscsi
->cmd_q
.index
);
926 rc
= vscsi
->flags
& SCHEDULE_DISCONNECT
;
927 crq
= vscsi
->cmd_q
.base_addr
+ vscsi
->cmd_q
.index
;
934 (vscsi
->cmd_q
.index
+ 1) & vscsi
->cmd_q
.mask
;
937 rc
= ibmvscsis_parse_command(vscsi
, crq
);
939 if ((uint
)crq
->valid
== VALID_TRANS_EVENT
) {
941 * must service the transport layer events even
942 * in an error state, dont break out until all
943 * the consecutive transport events have been
946 rc
= ibmvscsis_trans_event(vscsi
, crq
);
947 } else if (vscsi
->flags
& TRANS_EVENT
) {
949 * if a tranport event has occurred leave
950 * everything but transport events on the queue
952 pr_debug("poll_cmd_q, ignoring\n");
955 * need to decrement the queue index so we can
956 * look at the elment again
958 if (vscsi
->cmd_q
.index
)
959 vscsi
->cmd_q
.index
-= 1;
962 * index is at 0 it just wrapped.
963 * have it index last element in q
965 vscsi
->cmd_q
.index
= vscsi
->cmd_q
.mask
;
970 crq
->valid
= INVALIDATE_CMD_RESP_EL
;
972 crq
= vscsi
->cmd_q
.base_addr
+ vscsi
->cmd_q
.index
;
979 vio_enable_interrupts(vscsi
->dma_dev
);
981 pr_debug("poll_cmd_q, reenabling interrupts\n");
989 pr_debug("Leaving poll_cmd_q: rc %ld\n", rc
);
993 * ibmvscsis_free_cmd_qs() - Free elements in queue
994 * @vscsi: Pointer to our adapter structure
996 * Free all of the elements on all queues that are waiting for
1000 * Called with interrupt lock held
1002 static void ibmvscsis_free_cmd_qs(struct scsi_info
*vscsi
)
1004 struct ibmvscsis_cmd
*cmd
, *nxt
;
1006 pr_debug("free_cmd_qs: waiting_rsp empty %d, timer starter %d\n",
1007 (int)list_empty(&vscsi
->waiting_rsp
),
1008 vscsi
->rsp_q_timer
.started
);
1010 list_for_each_entry_safe(cmd
, nxt
, &vscsi
->waiting_rsp
, list
) {
1011 list_del(&cmd
->list
);
1012 ibmvscsis_free_cmd_resources(vscsi
, cmd
);
1017 * ibmvscsis_get_free_cmd() - Get free command from list
1018 * @vscsi: Pointer to our adapter structure
1020 * Must be called with interrupt lock held.
1022 static struct ibmvscsis_cmd
*ibmvscsis_get_free_cmd(struct scsi_info
*vscsi
)
1024 struct ibmvscsis_cmd
*cmd
= NULL
;
1025 struct iu_entry
*iue
;
1027 iue
= srp_iu_get(&vscsi
->target
);
1029 cmd
= list_first_entry_or_null(&vscsi
->free_cmd
,
1030 struct ibmvscsis_cmd
, list
);
1032 list_del(&cmd
->list
);
1034 cmd
->type
= UNSET_TYPE
;
1035 memset(&cmd
->se_cmd
, 0, sizeof(cmd
->se_cmd
));
1045 * ibmvscsis_adapter_idle() - Helper function to handle idle adapter
1046 * @vscsi: Pointer to our adapter structure
1048 * This function is called when the adapter is idle when the driver
1049 * is attempting to clear an error condition.
1050 * The adapter is considered busy if any of its cmd queues
1051 * are non-empty. This function can be invoked
1052 * from the off level disconnect function.
1054 * EXECUTION ENVIRONMENT:
1055 * Process environment called with interrupt lock held
1057 static void ibmvscsis_adapter_idle(struct scsi_info
*vscsi
)
1059 int free_qs
= false;
1061 pr_debug("adapter_idle: flags 0x%x, state 0x%hx\n", vscsi
->flags
,
1064 /* Only need to free qs if we're disconnecting from client */
1065 if (vscsi
->state
!= WAIT_CONNECTION
|| vscsi
->flags
& TRANS_EVENT
)
1068 switch (vscsi
->state
) {
1069 case ERR_DISCONNECT_RECONNECT
:
1070 ibmvscsis_reset_queue(vscsi
, WAIT_CONNECTION
);
1071 pr_debug("adapter_idle, disc_rec: flags 0x%x\n", vscsi
->flags
);
1074 case ERR_DISCONNECT
:
1075 ibmvscsis_free_command_q(vscsi
);
1076 vscsi
->flags
&= ~DISCONNECT_SCHEDULED
;
1077 vscsi
->flags
|= RESPONSE_Q_DOWN
;
1078 vscsi
->state
= ERR_DISCONNECTED
;
1079 pr_debug("adapter_idle, disc: flags 0x%x, state 0x%hx\n",
1080 vscsi
->flags
, vscsi
->state
);
1084 vscsi
->rsp_q_timer
.timer_pops
= 0;
1087 if (vscsi
->flags
& TRANS_EVENT
) {
1088 vscsi
->state
= WAIT_CONNECTION
;
1089 vscsi
->flags
&= PRESERVE_FLAG_FIELDS
;
1091 vscsi
->state
= CONNECTED
;
1092 vscsi
->flags
&= ~DISCONNECT_SCHEDULED
;
1095 pr_debug("adapter_idle, wait: flags 0x%x, state 0x%hx\n",
1096 vscsi
->flags
, vscsi
->state
);
1097 ibmvscsis_poll_cmd_q(vscsi
);
1100 case ERR_DISCONNECTED
:
1101 vscsi
->flags
&= ~DISCONNECT_SCHEDULED
;
1102 pr_debug("adapter_idle, disconnected: flags 0x%x, state 0x%hx\n",
1103 vscsi
->flags
, vscsi
->state
);
1107 dev_err(&vscsi
->dev
, "adapter_idle: in invalid state %d\n",
1113 ibmvscsis_free_cmd_qs(vscsi
);
1116 * There is a timing window where we could lose a disconnect request.
1117 * The known path to this window occurs during the DISCONNECT_RECONNECT
1118 * case above: reset_queue calls free_command_q, which will release the
1119 * interrupt lock. During that time, a new post_disconnect call can be
1120 * made with a "more severe" state (DISCONNECT or UNCONFIGURING).
1121 * Because the DISCONNECT_SCHEDULED flag is already set, post_disconnect
1122 * will only set the new_state. Now free_command_q reacquires the intr
1123 * lock and clears the DISCONNECT_SCHEDULED flag (using PRESERVE_FLAG_
1124 * FIELDS), and the disconnect is lost. This is particularly bad when
1125 * the new disconnect was for UNCONFIGURING, since the unconfigure hangs
1127 * Fix is that free command queue sets acr state and acr flags if there
1128 * is a change under the lock
1129 * note free command queue writes to this state it clears it
1130 * before releasing the lock, different drivers call the free command
1131 * queue different times so dont initialize above
1133 if (vscsi
->phyp_acr_state
!= 0) {
1135 * set any bits in flags that may have been cleared by
1136 * a call to free command queue in switch statement
1139 vscsi
->flags
|= vscsi
->phyp_acr_flags
;
1140 ibmvscsis_post_disconnect(vscsi
, vscsi
->phyp_acr_state
, 0);
1141 vscsi
->phyp_acr_state
= 0;
1142 vscsi
->phyp_acr_flags
= 0;
1144 pr_debug("adapter_idle: flags 0x%x, state 0x%hx, acr_flags 0x%x, acr_state 0x%hx\n",
1145 vscsi
->flags
, vscsi
->state
, vscsi
->phyp_acr_flags
,
1146 vscsi
->phyp_acr_state
);
1149 pr_debug("Leaving adapter_idle: flags 0x%x, state 0x%hx, new_state 0x%x\n",
1150 vscsi
->flags
, vscsi
->state
, vscsi
->new_state
);
1154 * ibmvscsis_copy_crq_packet() - Copy CRQ Packet
1155 * @vscsi: Pointer to our adapter structure
1156 * @cmd: Pointer to command element to use to process the request
1157 * @crq: Pointer to CRQ entry containing the request
1159 * Copy the srp information unit from the hosted
1160 * partition using remote dma
1162 * EXECUTION ENVIRONMENT:
1163 * Interrupt, interrupt lock held
1165 static long ibmvscsis_copy_crq_packet(struct scsi_info
*vscsi
,
1166 struct ibmvscsis_cmd
*cmd
,
1167 struct viosrp_crq
*crq
)
1169 struct iu_entry
*iue
= cmd
->iue
;
1173 len
= be16_to_cpu(crq
->IU_length
);
1174 if ((len
> SRP_MAX_IU_LEN
) || (len
== 0)) {
1175 dev_err(&vscsi
->dev
, "copy_crq: Invalid len %d passed", len
);
1176 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
1177 return SRP_VIOLATION
;
1180 rc
= h_copy_rdma(len
, vscsi
->dds
.window
[REMOTE
].liobn
,
1181 be64_to_cpu(crq
->IU_data_ptr
),
1182 vscsi
->dds
.window
[LOCAL
].liobn
, iue
->sbuf
->dma
);
1186 cmd
->init_time
= mftb();
1187 iue
->remote_token
= crq
->IU_data_ptr
;
1189 pr_debug("copy_crq: ioba 0x%llx, init_time 0x%llx\n",
1190 be64_to_cpu(crq
->IU_data_ptr
), cmd
->init_time
);
1193 if (connection_broken(vscsi
))
1194 ibmvscsis_post_disconnect(vscsi
,
1195 ERR_DISCONNECT_RECONNECT
,
1199 ibmvscsis_post_disconnect(vscsi
,
1200 ERR_DISCONNECT_RECONNECT
, 0);
1202 dev_err(&vscsi
->dev
, "copy_crq: h_copy_rdma failed, rc %ld\n",
1208 dev_err(&vscsi
->dev
, "copy_crq: h_copy_rdma failed, rc %ld\n",
1210 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
1218 * ibmvscsis_adapter_info - Service an Adapter Info MAnagement Data gram
1219 * @vscsi: Pointer to our adapter structure
1220 * @iue: Information Unit containing the Adapter Info MAD request
1222 * EXECUTION ENVIRONMENT:
1223 * Interrupt adpater lock is held
1225 static long ibmvscsis_adapter_info(struct scsi_info
*vscsi
,
1226 struct iu_entry
*iue
)
1228 struct viosrp_adapter_info
*mad
= &vio_iu(iue
)->mad
.adapter_info
;
1229 struct mad_adapter_info_data
*info
;
1234 mad
->common
.status
= cpu_to_be16(VIOSRP_MAD_SUCCESS
);
1236 if (be16_to_cpu(mad
->common
.length
) > sizeof(*info
)) {
1237 mad
->common
.status
= cpu_to_be16(VIOSRP_MAD_FAILED
);
1241 info
= dma_alloc_coherent(&vscsi
->dma_dev
->dev
, sizeof(*info
), &token
,
1244 dev_err(&vscsi
->dev
, "bad dma_alloc_coherent %p\n",
1246 mad
->common
.status
= cpu_to_be16(VIOSRP_MAD_FAILED
);
1250 /* Get remote info */
1251 rc
= h_copy_rdma(be16_to_cpu(mad
->common
.length
),
1252 vscsi
->dds
.window
[REMOTE
].liobn
,
1253 be64_to_cpu(mad
->buffer
),
1254 vscsi
->dds
.window
[LOCAL
].liobn
, token
);
1256 if (rc
!= H_SUCCESS
) {
1257 if (rc
== H_PERMISSION
) {
1258 if (connection_broken(vscsi
))
1259 flag_bits
= (RESPONSE_Q_DOWN
| CLIENT_FAILED
);
1261 pr_warn("adapter_info: h_copy_rdma from client failed, rc %ld\n",
1263 pr_debug("adapter_info: ioba 0x%llx, flags 0x%x, flag_bits 0x%x\n",
1264 be64_to_cpu(mad
->buffer
), vscsi
->flags
, flag_bits
);
1265 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
,
1271 * Copy client info, but ignore partition number, which we
1272 * already got from phyp - unless we failed to get it from
1273 * phyp (e.g. if we're running on a p5 system).
1275 if (vscsi
->client_data
.partition_number
== 0)
1276 vscsi
->client_data
.partition_number
=
1277 be32_to_cpu(info
->partition_number
);
1278 strncpy(vscsi
->client_data
.srp_version
, info
->srp_version
,
1279 sizeof(vscsi
->client_data
.srp_version
));
1280 strncpy(vscsi
->client_data
.partition_name
, info
->partition_name
,
1281 sizeof(vscsi
->client_data
.partition_name
));
1282 vscsi
->client_data
.mad_version
= be32_to_cpu(info
->mad_version
);
1283 vscsi
->client_data
.os_type
= be32_to_cpu(info
->os_type
);
1286 strncpy(info
->srp_version
, SRP_VERSION
,
1287 sizeof(info
->srp_version
));
1288 strncpy(info
->partition_name
, vscsi
->dds
.partition_name
,
1289 sizeof(info
->partition_name
));
1290 info
->partition_number
= cpu_to_be32(vscsi
->dds
.partition_num
);
1291 info
->mad_version
= cpu_to_be32(MAD_VERSION_1
);
1292 info
->os_type
= cpu_to_be32(LINUX
);
1293 memset(&info
->port_max_txu
[0], 0, sizeof(info
->port_max_txu
));
1294 info
->port_max_txu
[0] = cpu_to_be32(128 * PAGE_SIZE
);
1297 rc
= h_copy_rdma(sizeof(*info
), vscsi
->dds
.window
[LOCAL
].liobn
,
1298 token
, vscsi
->dds
.window
[REMOTE
].liobn
,
1299 be64_to_cpu(mad
->buffer
));
1307 if (connection_broken(vscsi
))
1308 flag_bits
= (RESPONSE_Q_DOWN
| CLIENT_FAILED
);
1310 dev_err(&vscsi
->dev
, "adapter_info: h_copy_rdma to client failed, rc %ld\n",
1312 ibmvscsis_post_disconnect(vscsi
,
1313 ERR_DISCONNECT_RECONNECT
,
1319 dma_free_coherent(&vscsi
->dma_dev
->dev
, sizeof(*info
), info
, token
);
1320 pr_debug("Leaving adapter_info, rc %ld\n", rc
);
1326 * ibmvscsis_cap_mad() - Service a Capabilities MAnagement Data gram
1327 * @vscsi: Pointer to our adapter structure
1328 * @iue: Information Unit containing the Capabilities MAD request
1330 * NOTE: if you return an error from this routine you must be
1331 * disconnecting or you will cause a hang
1333 * EXECUTION ENVIRONMENT:
1334 * Interrupt called with adapter lock held
1336 static int ibmvscsis_cap_mad(struct scsi_info
*vscsi
, struct iu_entry
*iue
)
1338 struct viosrp_capabilities
*mad
= &vio_iu(iue
)->mad
.capabilities
;
1339 struct capabilities
*cap
;
1340 struct mad_capability_common
*common
;
1342 u16 olen
, len
, status
, min_len
, cap_len
;
1347 olen
= be16_to_cpu(mad
->common
.length
);
1349 * struct capabilities hardcodes a couple capabilities after the
1350 * header, but the capabilities can actually be in any order.
1352 min_len
= offsetof(struct capabilities
, migration
);
1353 if ((olen
< min_len
) || (olen
> PAGE_SIZE
)) {
1354 pr_warn("cap_mad: invalid len %d\n", olen
);
1355 mad
->common
.status
= cpu_to_be16(VIOSRP_MAD_FAILED
);
1359 cap
= dma_alloc_coherent(&vscsi
->dma_dev
->dev
, olen
, &token
,
1362 dev_err(&vscsi
->dev
, "bad dma_alloc_coherent %p\n",
1364 mad
->common
.status
= cpu_to_be16(VIOSRP_MAD_FAILED
);
1367 rc
= h_copy_rdma(olen
, vscsi
->dds
.window
[REMOTE
].liobn
,
1368 be64_to_cpu(mad
->buffer
),
1369 vscsi
->dds
.window
[LOCAL
].liobn
, token
);
1370 if (rc
== H_SUCCESS
) {
1371 strncpy(cap
->name
, dev_name(&vscsi
->dma_dev
->dev
),
1374 len
= olen
- min_len
;
1375 status
= VIOSRP_MAD_SUCCESS
;
1376 common
= (struct mad_capability_common
*)&cap
->migration
;
1378 while ((len
> 0) && (status
== VIOSRP_MAD_SUCCESS
) && !rc
) {
1379 pr_debug("cap_mad: len left %hd, cap type %d, cap len %hd\n",
1380 len
, be32_to_cpu(common
->cap_type
),
1381 be16_to_cpu(common
->length
));
1383 cap_len
= be16_to_cpu(common
->length
);
1384 if (cap_len
> len
) {
1385 dev_err(&vscsi
->dev
, "cap_mad: cap len mismatch with total len\n");
1386 status
= VIOSRP_MAD_FAILED
;
1391 dev_err(&vscsi
->dev
, "cap_mad: cap len is 0\n");
1392 status
= VIOSRP_MAD_FAILED
;
1396 switch (common
->cap_type
) {
1398 pr_debug("cap_mad: unsupported capability\n");
1399 common
->server_support
= 0;
1400 flag
= cpu_to_be32((u32
)CAP_LIST_SUPPORTED
);
1401 cap
->flags
&= ~flag
;
1405 len
= len
- cap_len
;
1406 common
= (struct mad_capability_common
*)
1407 ((char *)common
+ cap_len
);
1410 mad
->common
.status
= cpu_to_be16(status
);
1413 rc
= h_copy_rdma(olen
, vscsi
->dds
.window
[LOCAL
].liobn
, token
,
1414 vscsi
->dds
.window
[REMOTE
].liobn
,
1415 be64_to_cpu(mad
->buffer
));
1417 if (rc
!= H_SUCCESS
) {
1418 pr_debug("cap_mad: failed to copy to client, rc %ld\n",
1421 if (rc
== H_PERMISSION
) {
1422 if (connection_broken(vscsi
))
1423 flag_bits
= (RESPONSE_Q_DOWN
|
1427 pr_warn("cap_mad: error copying data to client, rc %ld\n",
1429 ibmvscsis_post_disconnect(vscsi
,
1430 ERR_DISCONNECT_RECONNECT
,
1435 dma_free_coherent(&vscsi
->dma_dev
->dev
, olen
, cap
, token
);
1437 pr_debug("Leaving cap_mad, rc %ld, client_cap 0x%x\n",
1438 rc
, vscsi
->client_cap
);
1444 * ibmvscsis_process_mad() - Service a MAnagement Data gram
1445 * @vscsi: Pointer to our adapter structure
1446 * @iue: Information Unit containing the MAD request
1448 * Must be called with interrupt lock held.
1450 static long ibmvscsis_process_mad(struct scsi_info
*vscsi
, struct iu_entry
*iue
)
1452 struct mad_common
*mad
= (struct mad_common
*)&vio_iu(iue
)->mad
;
1453 struct viosrp_empty_iu
*empty
;
1454 long rc
= ADAPT_SUCCESS
;
1456 switch (be32_to_cpu(mad
->type
)) {
1457 case VIOSRP_EMPTY_IU_TYPE
:
1458 empty
= &vio_iu(iue
)->mad
.empty_iu
;
1459 vscsi
->empty_iu_id
= be64_to_cpu(empty
->buffer
);
1460 vscsi
->empty_iu_tag
= be64_to_cpu(empty
->common
.tag
);
1461 mad
->status
= cpu_to_be16(VIOSRP_MAD_SUCCESS
);
1463 case VIOSRP_ADAPTER_INFO_TYPE
:
1464 rc
= ibmvscsis_adapter_info(vscsi
, iue
);
1466 case VIOSRP_CAPABILITIES_TYPE
:
1467 rc
= ibmvscsis_cap_mad(vscsi
, iue
);
1469 case VIOSRP_ENABLE_FAST_FAIL
:
1470 if (vscsi
->state
== CONNECTED
) {
1471 vscsi
->fast_fail
= true;
1472 mad
->status
= cpu_to_be16(VIOSRP_MAD_SUCCESS
);
1474 pr_warn("fast fail mad sent after login\n");
1475 mad
->status
= cpu_to_be16(VIOSRP_MAD_FAILED
);
1479 mad
->status
= cpu_to_be16(VIOSRP_MAD_NOT_SUPPORTED
);
1487 * srp_snd_msg_failed() - Handle an error when sending a response
1488 * @vscsi: Pointer to our adapter structure
1489 * @rc: The return code from the h_send_crq command
1491 * Must be called with interrupt lock held.
1493 static void srp_snd_msg_failed(struct scsi_info
*vscsi
, long rc
)
1497 if (rc
!= H_DROPPED
) {
1498 ibmvscsis_free_cmd_qs(vscsi
);
1501 vscsi
->flags
|= CLIENT_FAILED
;
1503 /* don't flag the same problem multiple times */
1504 if (!(vscsi
->flags
& RESPONSE_Q_DOWN
)) {
1505 vscsi
->flags
|= RESPONSE_Q_DOWN
;
1506 if (!(vscsi
->state
& (ERR_DISCONNECT
|
1507 ERR_DISCONNECT_RECONNECT
|
1508 ERR_DISCONNECTED
| UNDEFINED
))) {
1509 dev_err(&vscsi
->dev
, "snd_msg_failed: setting RESPONSE_Q_DOWN, state 0x%hx, flags 0x%x, rc %ld\n",
1510 vscsi
->state
, vscsi
->flags
, rc
);
1512 ibmvscsis_post_disconnect(vscsi
,
1513 ERR_DISCONNECT_RECONNECT
, 0);
1519 * The response queue is full.
1520 * If the server is processing SRP requests, i.e.
1521 * the client has successfully done an
1522 * SRP_LOGIN, then it will wait forever for room in
1523 * the queue. However if the system admin
1524 * is attempting to unconfigure the server then one
1525 * or more children will be in a state where
1526 * they are being removed. So if there is even one
1527 * child being removed then the driver assumes
1528 * the system admin is attempting to break the
1529 * connection with the client and MAX_TIMER_POPS
1532 if ((vscsi
->rsp_q_timer
.timer_pops
< MAX_TIMER_POPS
) ||
1533 (vscsi
->state
== SRP_PROCESSING
)) {
1534 pr_debug("snd_msg_failed: response queue full, flags 0x%x, timer started %d, pops %d\n",
1535 vscsi
->flags
, (int)vscsi
->rsp_q_timer
.started
,
1536 vscsi
->rsp_q_timer
.timer_pops
);
1539 * Check if the timer is running; if it
1540 * is not then start it up.
1542 if (!vscsi
->rsp_q_timer
.started
) {
1543 if (vscsi
->rsp_q_timer
.timer_pops
<
1545 kt
= ktime_set(0, WAIT_NANO_SECONDS
);
1548 * slide the timeslice if the maximum
1549 * timer pops have already happened
1551 kt
= ktime_set(WAIT_SECONDS
, 0);
1554 vscsi
->rsp_q_timer
.started
= true;
1555 hrtimer_start(&vscsi
->rsp_q_timer
.timer
, kt
,
1560 * TBD: Do we need to worry about this? Need to get
1564 * waited a long time and it appears the system admin
1565 * is bring this driver down
1567 vscsi
->flags
|= RESPONSE_Q_DOWN
;
1568 ibmvscsis_free_cmd_qs(vscsi
);
1570 * if the driver is already attempting to disconnect
1571 * from the client and has already logged an error
1572 * trace this event but don't put it in the error log
1574 if (!(vscsi
->state
& (ERR_DISCONNECT
|
1575 ERR_DISCONNECT_RECONNECT
|
1576 ERR_DISCONNECTED
| UNDEFINED
))) {
1577 dev_err(&vscsi
->dev
, "client crq full too long\n");
1578 ibmvscsis_post_disconnect(vscsi
,
1579 ERR_DISCONNECT_RECONNECT
,
1586 * ibmvscsis_send_messages() - Send a Response
1587 * @vscsi: Pointer to our adapter structure
1589 * Send a response, first checking the waiting queue. Responses are
1590 * sent in order they are received. If the response cannot be sent,
1591 * because the client queue is full, it stays on the waiting queue.
1594 * Called with interrupt lock held
1596 static void ibmvscsis_send_messages(struct scsi_info
*vscsi
)
1599 /* note do not attmempt to access the IU_data_ptr with this pointer
1602 struct viosrp_crq
*crq
= (struct viosrp_crq
*)&msg_hi
;
1603 struct ibmvscsis_cmd
*cmd
, *nxt
;
1604 struct iu_entry
*iue
;
1605 long rc
= ADAPT_SUCCESS
;
1607 if (!(vscsi
->flags
& RESPONSE_Q_DOWN
)) {
1608 list_for_each_entry_safe(cmd
, nxt
, &vscsi
->waiting_rsp
, list
) {
1609 pr_debug("send_messages cmd %p\n", cmd
);
1613 crq
->valid
= VALID_CMD_RESP_EL
;
1614 crq
->format
= cmd
->rsp
.format
;
1616 if (cmd
->flags
& CMD_FAST_FAIL
)
1617 crq
->status
= VIOSRP_ADAPTER_FAIL
;
1619 crq
->IU_length
= cpu_to_be16(cmd
->rsp
.len
);
1621 rc
= h_send_crq(vscsi
->dma_dev
->unit_address
,
1622 be64_to_cpu(msg_hi
),
1623 be64_to_cpu(cmd
->rsp
.tag
));
1625 pr_debug("send_messages: tag 0x%llx, rc %ld\n",
1626 be64_to_cpu(cmd
->rsp
.tag
), rc
);
1628 /* if all ok free up the command element resources */
1629 if (rc
== H_SUCCESS
) {
1630 /* some movement has occurred */
1631 vscsi
->rsp_q_timer
.timer_pops
= 0;
1632 list_del(&cmd
->list
);
1634 ibmvscsis_free_cmd_resources(vscsi
, cmd
);
1636 srp_snd_msg_failed(vscsi
, rc
);
1643 * The timer could pop with the queue empty. If
1644 * this happens, rc will always indicate a
1645 * success; clear the pop count.
1647 vscsi
->rsp_q_timer
.timer_pops
= 0;
1650 ibmvscsis_free_cmd_qs(vscsi
);
1654 /* Called with intr lock held */
1655 static void ibmvscsis_send_mad_resp(struct scsi_info
*vscsi
,
1656 struct ibmvscsis_cmd
*cmd
,
1657 struct viosrp_crq
*crq
)
1659 struct iu_entry
*iue
= cmd
->iue
;
1660 struct mad_common
*mad
= (struct mad_common
*)&vio_iu(iue
)->mad
;
1665 rc
= h_copy_rdma(sizeof(struct mad_common
),
1666 vscsi
->dds
.window
[LOCAL
].liobn
, iue
->sbuf
->dma
,
1667 vscsi
->dds
.window
[REMOTE
].liobn
,
1668 be64_to_cpu(crq
->IU_data_ptr
));
1670 cmd
->rsp
.format
= VIOSRP_MAD_FORMAT
;
1671 cmd
->rsp
.len
= sizeof(struct mad_common
);
1672 cmd
->rsp
.tag
= mad
->tag
;
1673 list_add_tail(&cmd
->list
, &vscsi
->waiting_rsp
);
1674 ibmvscsis_send_messages(vscsi
);
1676 pr_debug("Error sending mad response, rc %ld\n", rc
);
1677 if (rc
== H_PERMISSION
) {
1678 if (connection_broken(vscsi
))
1679 flag_bits
= (RESPONSE_Q_DOWN
| CLIENT_FAILED
);
1681 dev_err(&vscsi
->dev
, "mad: failed to copy to client, rc %ld\n",
1684 ibmvscsis_free_cmd_resources(vscsi
, cmd
);
1685 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
,
1691 * ibmvscsis_mad() - Service a MAnagement Data gram.
1692 * @vscsi: Pointer to our adapter structure
1693 * @crq: Pointer to the CRQ entry containing the MAD request
1695 * EXECUTION ENVIRONMENT:
1696 * Interrupt called with adapter lock held
1698 static long ibmvscsis_mad(struct scsi_info
*vscsi
, struct viosrp_crq
*crq
)
1700 struct iu_entry
*iue
;
1701 struct ibmvscsis_cmd
*cmd
;
1702 struct mad_common
*mad
;
1703 long rc
= ADAPT_SUCCESS
;
1705 switch (vscsi
->state
) {
1707 * We have not exchanged Init Msgs yet, so this MAD was sent
1708 * before the last Transport Event; client will not be
1709 * expecting a response.
1711 case WAIT_CONNECTION
:
1712 pr_debug("mad: in Wait Connection state, ignoring MAD, flags %d\n",
1714 return ADAPT_SUCCESS
;
1716 case SRP_PROCESSING
:
1721 * We should never get here while we're in these states.
1722 * Just log an error and get out.
1726 case ERR_DISCONNECT
:
1727 case ERR_DISCONNECT_RECONNECT
:
1729 dev_err(&vscsi
->dev
, "mad: invalid adapter state %d for mad\n",
1731 return ADAPT_SUCCESS
;
1734 cmd
= ibmvscsis_get_free_cmd(vscsi
);
1736 dev_err(&vscsi
->dev
, "mad: failed to get cmd, debit %d\n",
1738 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
1742 cmd
->type
= ADAPTER_MAD
;
1744 rc
= ibmvscsis_copy_crq_packet(vscsi
, cmd
, crq
);
1746 mad
= (struct mad_common
*)&vio_iu(iue
)->mad
;
1748 pr_debug("mad: type %d\n", be32_to_cpu(mad
->type
));
1750 if (be16_to_cpu(mad
->length
) < 0) {
1751 dev_err(&vscsi
->dev
, "mad: length is < 0\n");
1752 ibmvscsis_post_disconnect(vscsi
,
1753 ERR_DISCONNECT_RECONNECT
, 0);
1756 rc
= ibmvscsis_process_mad(vscsi
, iue
);
1759 pr_debug("mad: status %hd, rc %ld\n", be16_to_cpu(mad
->status
),
1763 ibmvscsis_send_mad_resp(vscsi
, cmd
, crq
);
1765 ibmvscsis_free_cmd_resources(vscsi
, cmd
);
1768 pr_debug("Leaving mad, rc %ld\n", rc
);
1773 * ibmvscsis_login_rsp() - Create/copy a login response notice to the client
1774 * @vscsi: Pointer to our adapter structure
1775 * @cmd: Pointer to the command for the SRP Login request
1777 * EXECUTION ENVIRONMENT:
1778 * Interrupt, interrupt lock held
1780 static long ibmvscsis_login_rsp(struct scsi_info
*vscsi
,
1781 struct ibmvscsis_cmd
*cmd
)
1783 struct iu_entry
*iue
= cmd
->iue
;
1784 struct srp_login_rsp
*rsp
= &vio_iu(iue
)->srp
.login_rsp
;
1785 struct format_code
*fmt
;
1787 long rc
= ADAPT_SUCCESS
;
1789 memset(rsp
, 0, sizeof(struct srp_login_rsp
));
1791 rsp
->opcode
= SRP_LOGIN_RSP
;
1792 rsp
->req_lim_delta
= cpu_to_be32(vscsi
->request_limit
);
1793 rsp
->tag
= cmd
->rsp
.tag
;
1794 rsp
->max_it_iu_len
= cpu_to_be32(SRP_MAX_IU_LEN
);
1795 rsp
->max_ti_iu_len
= cpu_to_be32(SRP_MAX_IU_LEN
);
1796 fmt
= (struct format_code
*)&rsp
->buf_fmt
;
1797 fmt
->buffers
= SUPPORTED_FORMATS
;
1800 cmd
->rsp
.len
= sizeof(struct srp_login_rsp
);
1803 rc
= h_copy_rdma(cmd
->rsp
.len
, vscsi
->dds
.window
[LOCAL
].liobn
,
1804 iue
->sbuf
->dma
, vscsi
->dds
.window
[REMOTE
].liobn
,
1805 be64_to_cpu(iue
->remote_token
));
1812 if (connection_broken(vscsi
))
1813 flag_bits
= RESPONSE_Q_DOWN
| CLIENT_FAILED
;
1814 dev_err(&vscsi
->dev
, "login_rsp: error copying to client, rc %ld\n",
1816 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
,
1822 dev_err(&vscsi
->dev
, "login_rsp: error copying to client, rc %ld\n",
1824 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
1832 * ibmvscsis_srp_login_rej() - Create/copy a login rejection notice to client
1833 * @vscsi: Pointer to our adapter structure
1834 * @cmd: Pointer to the command for the SRP Login request
1835 * @reason: The reason the SRP Login is being rejected, per SRP protocol
1837 * EXECUTION ENVIRONMENT:
1838 * Interrupt, interrupt lock held
1840 static long ibmvscsis_srp_login_rej(struct scsi_info
*vscsi
,
1841 struct ibmvscsis_cmd
*cmd
, u32 reason
)
1843 struct iu_entry
*iue
= cmd
->iue
;
1844 struct srp_login_rej
*rej
= &vio_iu(iue
)->srp
.login_rej
;
1845 struct format_code
*fmt
;
1847 long rc
= ADAPT_SUCCESS
;
1849 memset(rej
, 0, sizeof(*rej
));
1851 rej
->opcode
= SRP_LOGIN_REJ
;
1852 rej
->reason
= cpu_to_be32(reason
);
1853 rej
->tag
= cmd
->rsp
.tag
;
1854 fmt
= (struct format_code
*)&rej
->buf_fmt
;
1855 fmt
->buffers
= SUPPORTED_FORMATS
;
1857 cmd
->rsp
.len
= sizeof(*rej
);
1860 rc
= h_copy_rdma(cmd
->rsp
.len
, vscsi
->dds
.window
[LOCAL
].liobn
,
1861 iue
->sbuf
->dma
, vscsi
->dds
.window
[REMOTE
].liobn
,
1862 be64_to_cpu(iue
->remote_token
));
1868 if (connection_broken(vscsi
))
1869 flag_bits
= RESPONSE_Q_DOWN
| CLIENT_FAILED
;
1870 dev_err(&vscsi
->dev
, "login_rej: error copying to client, rc %ld\n",
1872 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
,
1878 dev_err(&vscsi
->dev
, "login_rej: error copying to client, rc %ld\n",
1880 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
1887 static int ibmvscsis_make_nexus(struct ibmvscsis_tport
*tport
)
1889 char *name
= tport
->tport_name
;
1890 struct ibmvscsis_nexus
*nexus
;
1893 if (tport
->ibmv_nexus
) {
1894 pr_debug("tport->ibmv_nexus already exists\n");
1898 nexus
= kzalloc(sizeof(*nexus
), GFP_KERNEL
);
1900 pr_err("Unable to allocate struct ibmvscsis_nexus\n");
1904 nexus
->se_sess
= target_alloc_session(&tport
->se_tpg
, 0, 0,
1905 TARGET_PROT_NORMAL
, name
, nexus
,
1907 if (IS_ERR(nexus
->se_sess
)) {
1908 rc
= PTR_ERR(nexus
->se_sess
);
1909 goto transport_init_fail
;
1912 tport
->ibmv_nexus
= nexus
;
1916 transport_init_fail
:
1921 static int ibmvscsis_drop_nexus(struct ibmvscsis_tport
*tport
)
1923 struct se_session
*se_sess
;
1924 struct ibmvscsis_nexus
*nexus
;
1926 nexus
= tport
->ibmv_nexus
;
1930 se_sess
= nexus
->se_sess
;
1935 * Release the SCSI I_T Nexus to the emulated ibmvscsis Target Port
1937 transport_deregister_session(se_sess
);
1938 tport
->ibmv_nexus
= NULL
;
1945 * ibmvscsis_srp_login() - Process an SRP Login Request
1946 * @vscsi: Pointer to our adapter structure
1947 * @cmd: Command element to use to process the SRP Login request
1948 * @crq: Pointer to CRQ entry containing the SRP Login request
1950 * EXECUTION ENVIRONMENT:
1951 * Interrupt, called with interrupt lock held
1953 static long ibmvscsis_srp_login(struct scsi_info
*vscsi
,
1954 struct ibmvscsis_cmd
*cmd
,
1955 struct viosrp_crq
*crq
)
1957 struct iu_entry
*iue
= cmd
->iue
;
1958 struct srp_login_req
*req
= &vio_iu(iue
)->srp
.login_req
;
1960 __be64 id_extension
;
1963 struct format_code
*fmt
;
1965 long rc
= ADAPT_SUCCESS
;
1967 iport
= (struct port_id
*)req
->initiator_port_id
;
1968 tport
= (struct port_id
*)req
->target_port_id
;
1969 fmt
= (struct format_code
*)&req
->req_buf_fmt
;
1970 if (be32_to_cpu(req
->req_it_iu_len
) > SRP_MAX_IU_LEN
)
1971 reason
= SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE
;
1972 else if (be32_to_cpu(req
->req_it_iu_len
) < 64)
1973 reason
= SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL
;
1974 else if ((be64_to_cpu(iport
->id_extension
) > (MAX_NUM_PORTS
- 1)) ||
1975 (be64_to_cpu(tport
->id_extension
) > (MAX_NUM_PORTS
- 1)))
1976 reason
= SRP_LOGIN_REJ_UNABLE_ASSOCIATE_CHANNEL
;
1977 else if (req
->req_flags
& SRP_MULTICHAN_MULTI
)
1978 reason
= SRP_LOGIN_REJ_MULTI_CHANNEL_UNSUPPORTED
;
1979 else if (fmt
->buffers
& (~SUPPORTED_FORMATS
))
1980 reason
= SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT
;
1981 else if ((fmt
->buffers
| SUPPORTED_FORMATS
) == 0)
1982 reason
= SRP_LOGIN_REJ_UNSUPPORTED_DESCRIPTOR_FMT
;
1984 if (vscsi
->state
== SRP_PROCESSING
)
1985 reason
= SRP_LOGIN_REJ_CHANNEL_LIMIT_REACHED
;
1987 rc
= ibmvscsis_make_nexus(&vscsi
->tport
);
1989 reason
= SRP_LOGIN_REJ_UNABLE_ESTABLISH_CHANNEL
;
1991 cmd
->rsp
.format
= VIOSRP_SRP_FORMAT
;
1992 cmd
->rsp
.tag
= req
->tag
;
1994 pr_debug("srp_login: reason 0x%x\n", reason
);
1997 rc
= ibmvscsis_srp_login_rej(vscsi
, cmd
, reason
);
1999 rc
= ibmvscsis_login_rsp(vscsi
, cmd
);
2003 vscsi
->state
= SRP_PROCESSING
;
2005 list_add_tail(&cmd
->list
, &vscsi
->waiting_rsp
);
2006 ibmvscsis_send_messages(vscsi
);
2008 ibmvscsis_free_cmd_resources(vscsi
, cmd
);
2011 pr_debug("Leaving srp_login, rc %ld\n", rc
);
2016 * ibmvscsis_srp_i_logout() - Helper Function to close I_T Nexus
2017 * @vscsi: Pointer to our adapter structure
2018 * @cmd: Command element to use to process the Implicit Logout request
2019 * @crq: Pointer to CRQ entry containing the Implicit Logout request
2021 * Do the logic to close the I_T nexus. This function may not
2022 * behave to specification.
2024 * EXECUTION ENVIRONMENT:
2025 * Interrupt, interrupt lock held
2027 static long ibmvscsis_srp_i_logout(struct scsi_info
*vscsi
,
2028 struct ibmvscsis_cmd
*cmd
,
2029 struct viosrp_crq
*crq
)
2031 struct iu_entry
*iue
= cmd
->iue
;
2032 struct srp_i_logout
*log_out
= &vio_iu(iue
)->srp
.i_logout
;
2033 long rc
= ADAPT_SUCCESS
;
2035 if ((vscsi
->debit
> 0) || !list_empty(&vscsi
->schedule_q
) ||
2036 !list_empty(&vscsi
->waiting_rsp
)) {
2037 dev_err(&vscsi
->dev
, "i_logout: outstanding work\n");
2038 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT
, 0);
2040 cmd
->rsp
.format
= SRP_FORMAT
;
2041 cmd
->rsp
.tag
= log_out
->tag
;
2042 cmd
->rsp
.len
= sizeof(struct mad_common
);
2043 list_add_tail(&cmd
->list
, &vscsi
->waiting_rsp
);
2044 ibmvscsis_send_messages(vscsi
);
2046 ibmvscsis_post_disconnect(vscsi
, WAIT_IDLE
, 0);
2052 /* Called with intr lock held */
2053 static void ibmvscsis_srp_cmd(struct scsi_info
*vscsi
, struct viosrp_crq
*crq
)
2055 struct ibmvscsis_cmd
*cmd
;
2056 struct iu_entry
*iue
;
2057 struct srp_cmd
*srp
;
2058 struct srp_tsk_mgmt
*tsk
;
2061 if (vscsi
->request_limit
- vscsi
->debit
<= 0) {
2062 /* Client has exceeded request limit */
2063 dev_err(&vscsi
->dev
, "Client exceeded the request limit (%d), debit %d\n",
2064 vscsi
->request_limit
, vscsi
->debit
);
2065 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
2069 cmd
= ibmvscsis_get_free_cmd(vscsi
);
2071 dev_err(&vscsi
->dev
, "srp_cmd failed to get cmd, debit %d\n",
2073 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
2077 srp
= &vio_iu(iue
)->srp
.cmd
;
2079 rc
= ibmvscsis_copy_crq_packet(vscsi
, cmd
, crq
);
2081 ibmvscsis_free_cmd_resources(vscsi
, cmd
);
2085 if (vscsi
->state
== SRP_PROCESSING
) {
2086 switch (srp
->opcode
) {
2088 rc
= ibmvscsis_srp_login(vscsi
, cmd
, crq
);
2092 tsk
= &vio_iu(iue
)->srp
.tsk_mgmt
;
2093 pr_debug("tsk_mgmt tag: %llu (0x%llx)\n", tsk
->tag
,
2095 cmd
->rsp
.tag
= tsk
->tag
;
2097 cmd
->type
= TASK_MANAGEMENT
;
2098 list_add_tail(&cmd
->list
, &vscsi
->schedule_q
);
2099 queue_work(vscsi
->work_q
, &cmd
->work
);
2103 pr_debug("srp_cmd tag: %llu (0x%llx)\n", srp
->tag
,
2105 cmd
->rsp
.tag
= srp
->tag
;
2107 cmd
->type
= SCSI_CDB
;
2109 * We want to keep track of work waiting for
2112 list_add_tail(&cmd
->list
, &vscsi
->schedule_q
);
2113 queue_work(vscsi
->work_q
, &cmd
->work
);
2117 rc
= ibmvscsis_srp_i_logout(vscsi
, cmd
, crq
);
2123 ibmvscsis_free_cmd_resources(vscsi
, cmd
);
2124 dev_err(&vscsi
->dev
, "invalid srp cmd, opcode %d\n",
2126 ibmvscsis_post_disconnect(vscsi
,
2127 ERR_DISCONNECT_RECONNECT
, 0);
2130 } else if (srp
->opcode
== SRP_LOGIN_REQ
&& vscsi
->state
== CONNECTED
) {
2131 rc
= ibmvscsis_srp_login(vscsi
, cmd
, crq
);
2133 ibmvscsis_free_cmd_resources(vscsi
, cmd
);
2134 dev_err(&vscsi
->dev
, "Invalid state %d to handle srp cmd\n",
2136 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
2141 * ibmvscsis_ping_response() - Respond to a ping request
2142 * @vscsi: Pointer to our adapter structure
2144 * Let the client know that the server is alive and waiting on
2145 * its native I/O stack.
2146 * If any type of error occurs from the call to queue a ping
2147 * response then the client is either not accepting or receiving
2148 * interrupts. Disconnect with an error.
2150 * EXECUTION ENVIRONMENT:
2151 * Interrupt, interrupt lock held
2153 static long ibmvscsis_ping_response(struct scsi_info
*vscsi
)
2155 struct viosrp_crq
*crq
;
2156 u64 buffer
[2] = { 0, 0 };
2159 crq
= (struct viosrp_crq
*)&buffer
;
2160 crq
->valid
= VALID_CMD_RESP_EL
;
2161 crq
->format
= (u8
)MESSAGE_IN_CRQ
;
2162 crq
->status
= PING_RESPONSE
;
2164 rc
= h_send_crq(vscsi
->dds
.unit_id
, cpu_to_be64(buffer
[MSG_HI
]),
2165 cpu_to_be64(buffer
[MSG_LOW
]));
2171 vscsi
->flags
|= CLIENT_FAILED
;
2173 vscsi
->flags
|= RESPONSE_Q_DOWN
;
2175 dev_err(&vscsi
->dev
, "ping_response: h_send_crq failed, rc %ld\n",
2177 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
2180 dev_err(&vscsi
->dev
, "ping_response: h_send_crq returned unknown rc %ld\n",
2182 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT
, 0);
2190 * ibmvscsis_handle_init_compl_msg() - Respond to an Init Complete Message
2191 * @vscsi: Pointer to our adapter structure
2193 * Must be called with interrupt lock held.
2195 static long ibmvscsis_handle_init_compl_msg(struct scsi_info
*vscsi
)
2197 long rc
= ADAPT_SUCCESS
;
2199 switch (vscsi
->state
) {
2201 case ERR_DISCONNECT
:
2202 case ERR_DISCONNECT_RECONNECT
:
2203 case ERR_DISCONNECTED
:
2209 case WAIT_CONNECTION
:
2210 vscsi
->state
= CONNECTED
;
2214 case SRP_PROCESSING
:
2217 case PART_UP_WAIT_ENAB
:
2220 dev_err(&vscsi
->dev
, "init_msg: invalid state %d to get init compl msg\n",
2222 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
2230 * ibmvscsis_handle_init_msg() - Respond to an Init Message
2231 * @vscsi: Pointer to our adapter structure
2233 * Must be called with interrupt lock held.
2235 static long ibmvscsis_handle_init_msg(struct scsi_info
*vscsi
)
2237 long rc
= ADAPT_SUCCESS
;
2239 switch (vscsi
->state
) {
2241 vscsi
->state
= PART_UP_WAIT_ENAB
;
2244 case WAIT_CONNECTION
:
2245 rc
= ibmvscsis_send_init_message(vscsi
, INIT_COMPLETE_MSG
);
2248 vscsi
->state
= CONNECTED
;
2252 dev_err(&vscsi
->dev
, "init_msg: failed to send, rc %ld\n",
2254 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT
, 0);
2258 dev_err(&vscsi
->dev
, "init_msg: failed to send, rc %ld\n",
2261 ibmvscsis_post_disconnect(vscsi
,
2262 ERR_DISCONNECT_RECONNECT
, 0);
2266 pr_warn("init_msg: failed to send, rc %ld\n", rc
);
2279 case PART_UP_WAIT_ENAB
:
2281 case SRP_PROCESSING
:
2284 case ERR_DISCONNECT
:
2285 case ERR_DISCONNECT_RECONNECT
:
2286 case ERR_DISCONNECTED
:
2289 dev_err(&vscsi
->dev
, "init_msg: invalid state %d to get init msg\n",
2291 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
2299 * ibmvscsis_init_msg() - Respond to an init message
2300 * @vscsi: Pointer to our adapter structure
2301 * @crq: Pointer to CRQ element containing the Init Message
2303 * EXECUTION ENVIRONMENT:
2304 * Interrupt, interrupt lock held
2306 static long ibmvscsis_init_msg(struct scsi_info
*vscsi
, struct viosrp_crq
*crq
)
2308 long rc
= ADAPT_SUCCESS
;
2310 pr_debug("init_msg: state 0x%hx\n", vscsi
->state
);
2312 rc
= h_vioctl(vscsi
->dds
.unit_id
, H_GET_PARTNER_INFO
,
2313 (u64
)vscsi
->map_ioba
| ((u64
)PAGE_SIZE
<< 32), 0, 0, 0,
2315 if (rc
== H_SUCCESS
) {
2316 vscsi
->client_data
.partition_number
=
2317 be64_to_cpu(*(u64
*)vscsi
->map_buf
);
2318 pr_debug("init_msg, part num %d\n",
2319 vscsi
->client_data
.partition_number
);
2321 pr_debug("init_msg h_vioctl rc %ld\n", rc
);
2325 if (crq
->format
== INIT_MSG
) {
2326 rc
= ibmvscsis_handle_init_msg(vscsi
);
2327 } else if (crq
->format
== INIT_COMPLETE_MSG
) {
2328 rc
= ibmvscsis_handle_init_compl_msg(vscsi
);
2331 dev_err(&vscsi
->dev
, "init_msg: invalid format %d\n",
2333 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
2340 * ibmvscsis_parse_command() - Parse an element taken from the cmd rsp queue.
2341 * @vscsi: Pointer to our adapter structure
2342 * @crq: Pointer to CRQ element containing the SRP request
2344 * This function will return success if the command queue element is valid
2345 * and the srp iu or MAD request it pointed to was also valid. That does
2346 * not mean that an error was not returned to the client.
2348 * EXECUTION ENVIRONMENT:
2349 * Interrupt, intr lock held
2351 static long ibmvscsis_parse_command(struct scsi_info
*vscsi
,
2352 struct viosrp_crq
*crq
)
2354 long rc
= ADAPT_SUCCESS
;
2356 switch (crq
->valid
) {
2357 case VALID_CMD_RESP_EL
:
2358 switch (crq
->format
) {
2363 if (vscsi
->flags
& PROCESSING_MAD
) {
2365 dev_err(&vscsi
->dev
, "parse_command: already processing mad\n");
2366 ibmvscsis_post_disconnect(vscsi
,
2367 ERR_DISCONNECT_RECONNECT
,
2370 vscsi
->flags
|= PROCESSING_MAD
;
2371 rc
= ibmvscsis_mad(vscsi
, crq
);
2376 ibmvscsis_srp_cmd(vscsi
, crq
);
2379 case MESSAGE_IN_CRQ
:
2380 if (crq
->status
== PING
)
2381 ibmvscsis_ping_response(vscsi
);
2385 dev_err(&vscsi
->dev
, "parse_command: invalid format %d\n",
2387 ibmvscsis_post_disconnect(vscsi
,
2388 ERR_DISCONNECT_RECONNECT
, 0);
2393 case VALID_TRANS_EVENT
:
2394 rc
= ibmvscsis_trans_event(vscsi
, crq
);
2397 case VALID_INIT_MSG
:
2398 rc
= ibmvscsis_init_msg(vscsi
, crq
);
2402 dev_err(&vscsi
->dev
, "parse_command: invalid valid field %d\n",
2404 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
2409 * Return only what the interrupt handler cares
2410 * about. Most errors we keep right on trucking.
2412 rc
= vscsi
->flags
& SCHEDULE_DISCONNECT
;
2417 static int read_dma_window(struct scsi_info
*vscsi
)
2419 struct vio_dev
*vdev
= vscsi
->dma_dev
;
2420 const __be32
*dma_window
;
2423 /* TODO Using of_parse_dma_window would be better, but it doesn't give
2424 * a way to read multiple windows without already knowing the size of
2425 * a window or the number of windows.
2427 dma_window
= (const __be32
*)vio_get_attribute(vdev
,
2428 "ibm,my-dma-window",
2431 pr_err("Couldn't find ibm,my-dma-window property\n");
2435 vscsi
->dds
.window
[LOCAL
].liobn
= be32_to_cpu(*dma_window
);
2438 prop
= (const __be32
*)vio_get_attribute(vdev
, "ibm,#dma-address-cells",
2441 pr_warn("Couldn't find ibm,#dma-address-cells property\n");
2444 dma_window
+= be32_to_cpu(*prop
);
2447 prop
= (const __be32
*)vio_get_attribute(vdev
, "ibm,#dma-size-cells",
2450 pr_warn("Couldn't find ibm,#dma-size-cells property\n");
2453 dma_window
+= be32_to_cpu(*prop
);
2456 /* dma_window should point to the second window now */
2457 vscsi
->dds
.window
[REMOTE
].liobn
= be32_to_cpu(*dma_window
);
2462 static struct ibmvscsis_tport
*ibmvscsis_lookup_port(const char *name
)
2464 struct ibmvscsis_tport
*tport
= NULL
;
2465 struct vio_dev
*vdev
;
2466 struct scsi_info
*vscsi
;
2468 spin_lock_bh(&ibmvscsis_dev_lock
);
2469 list_for_each_entry(vscsi
, &ibmvscsis_dev_list
, list
) {
2470 vdev
= vscsi
->dma_dev
;
2471 if (!strcmp(dev_name(&vdev
->dev
), name
)) {
2472 tport
= &vscsi
->tport
;
2476 spin_unlock_bh(&ibmvscsis_dev_lock
);
2482 * ibmvscsis_parse_cmd() - Parse SRP Command
2483 * @vscsi: Pointer to our adapter structure
2484 * @cmd: Pointer to command element with SRP command
2486 * Parse the srp command; if it is valid then submit it to tcm.
2487 * Note: The return code does not reflect the status of the SCSI CDB.
2489 * EXECUTION ENVIRONMENT:
2492 static void ibmvscsis_parse_cmd(struct scsi_info
*vscsi
,
2493 struct ibmvscsis_cmd
*cmd
)
2495 struct iu_entry
*iue
= cmd
->iue
;
2496 struct srp_cmd
*srp
= (struct srp_cmd
*)iue
->sbuf
->buf
;
2497 struct ibmvscsis_nexus
*nexus
;
2499 enum dma_data_direction dir
;
2503 nexus
= vscsi
->tport
.ibmv_nexus
;
2505 * additional length in bytes. Note that the SRP spec says that
2506 * additional length is in 4-byte words, but technically the
2507 * additional length field is only the upper 6 bits of the byte.
2508 * The lower 2 bits are reserved. If the lower 2 bits are 0 (as
2509 * all reserved fields should be), then interpreting the byte as
2510 * an int will yield the length in bytes.
2512 if (srp
->add_cdb_len
& 0x03) {
2513 dev_err(&vscsi
->dev
, "parse_cmd: reserved bits set in IU\n");
2514 spin_lock_bh(&vscsi
->intr_lock
);
2515 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
2516 ibmvscsis_free_cmd_resources(vscsi
, cmd
);
2517 spin_unlock_bh(&vscsi
->intr_lock
);
2521 if (srp_get_desc_table(srp
, &dir
, &data_len
)) {
2522 dev_err(&vscsi
->dev
, "0x%llx: parsing SRP descriptor table failed.\n",
2528 cmd
->rsp
.sol_not
= srp
->sol_not
;
2530 switch (srp
->task_attr
) {
2531 case SRP_SIMPLE_TASK
:
2532 attr
= TCM_SIMPLE_TAG
;
2534 case SRP_ORDERED_TASK
:
2535 attr
= TCM_ORDERED_TAG
;
2538 attr
= TCM_HEAD_TAG
;
2544 dev_err(&vscsi
->dev
, "Invalid task attribute %d\n",
2549 cmd
->se_cmd
.tag
= be64_to_cpu(srp
->tag
);
2551 spin_lock_bh(&vscsi
->intr_lock
);
2552 list_add_tail(&cmd
->list
, &vscsi
->active_q
);
2553 spin_unlock_bh(&vscsi
->intr_lock
);
2555 srp
->lun
.scsi_lun
[0] &= 0x3f;
2557 pr_debug("calling submit_cmd, se_cmd %p, lun 0x%llx, cdb 0x%x, attr:%d\n",
2558 &cmd
->se_cmd
, scsilun_to_int(&srp
->lun
), (int)srp
->cdb
[0],
2561 rc
= target_submit_cmd(&cmd
->se_cmd
, nexus
->se_sess
, srp
->cdb
,
2562 cmd
->sense_buf
, scsilun_to_int(&srp
->lun
),
2563 data_len
, attr
, dir
, 0);
2565 dev_err(&vscsi
->dev
, "target_submit_cmd failed, rc %d\n", rc
);
2571 spin_lock_bh(&vscsi
->intr_lock
);
2572 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
2573 spin_unlock_bh(&vscsi
->intr_lock
);
2577 * ibmvscsis_parse_task() - Parse SRP Task Management Request
2578 * @vscsi: Pointer to our adapter structure
2579 * @cmd: Pointer to command element with SRP task management request
2581 * Parse the srp task management request; if it is valid then submit it to tcm.
2582 * Note: The return code does not reflect the status of the task management
2585 * EXECUTION ENVIRONMENT:
2588 static void ibmvscsis_parse_task(struct scsi_info
*vscsi
,
2589 struct ibmvscsis_cmd
*cmd
)
2591 struct iu_entry
*iue
= cmd
->iue
;
2592 struct srp_tsk_mgmt
*srp_tsk
= &vio_iu(iue
)->srp
.tsk_mgmt
;
2594 u64 tag_to_abort
= 0;
2596 struct ibmvscsis_nexus
*nexus
;
2598 nexus
= vscsi
->tport
.ibmv_nexus
;
2600 cmd
->rsp
.sol_not
= srp_tsk
->sol_not
;
2602 switch (srp_tsk
->tsk_mgmt_func
) {
2603 case SRP_TSK_ABORT_TASK
:
2604 tcm_type
= TMR_ABORT_TASK
;
2605 tag_to_abort
= be64_to_cpu(srp_tsk
->task_tag
);
2607 case SRP_TSK_ABORT_TASK_SET
:
2608 tcm_type
= TMR_ABORT_TASK_SET
;
2610 case SRP_TSK_CLEAR_TASK_SET
:
2611 tcm_type
= TMR_CLEAR_TASK_SET
;
2613 case SRP_TSK_LUN_RESET
:
2614 tcm_type
= TMR_LUN_RESET
;
2616 case SRP_TSK_CLEAR_ACA
:
2617 tcm_type
= TMR_CLEAR_ACA
;
2620 dev_err(&vscsi
->dev
, "unknown task mgmt func %d\n",
2621 srp_tsk
->tsk_mgmt_func
);
2622 cmd
->se_cmd
.se_tmr_req
->response
=
2623 TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
;
2629 cmd
->se_cmd
.tag
= be64_to_cpu(srp_tsk
->tag
);
2631 spin_lock_bh(&vscsi
->intr_lock
);
2632 list_add_tail(&cmd
->list
, &vscsi
->active_q
);
2633 spin_unlock_bh(&vscsi
->intr_lock
);
2635 srp_tsk
->lun
.scsi_lun
[0] &= 0x3f;
2637 pr_debug("calling submit_tmr, func %d\n",
2638 srp_tsk
->tsk_mgmt_func
);
2639 rc
= target_submit_tmr(&cmd
->se_cmd
, nexus
->se_sess
, NULL
,
2640 scsilun_to_int(&srp_tsk
->lun
), srp_tsk
,
2641 tcm_type
, GFP_KERNEL
, tag_to_abort
, 0);
2643 dev_err(&vscsi
->dev
, "target_submit_tmr failed, rc %d\n",
2645 cmd
->se_cmd
.se_tmr_req
->response
=
2646 TMR_FUNCTION_REJECTED
;
2651 transport_send_check_condition_and_sense(&cmd
->se_cmd
, 0, 0);
2654 static void ibmvscsis_scheduler(struct work_struct
*work
)
2656 struct ibmvscsis_cmd
*cmd
= container_of(work
, struct ibmvscsis_cmd
,
2658 struct scsi_info
*vscsi
= cmd
->adapter
;
2660 spin_lock_bh(&vscsi
->intr_lock
);
2662 /* Remove from schedule_q */
2663 list_del(&cmd
->list
);
2665 /* Don't submit cmd if we're disconnecting */
2666 if (vscsi
->flags
& (SCHEDULE_DISCONNECT
| DISCONNECT_SCHEDULED
)) {
2667 ibmvscsis_free_cmd_resources(vscsi
, cmd
);
2669 /* ibmvscsis_disconnect might be waiting for us */
2670 if (list_empty(&vscsi
->active_q
) &&
2671 list_empty(&vscsi
->schedule_q
) &&
2672 (vscsi
->flags
& WAIT_FOR_IDLE
)) {
2673 vscsi
->flags
&= ~WAIT_FOR_IDLE
;
2674 complete(&vscsi
->wait_idle
);
2677 spin_unlock_bh(&vscsi
->intr_lock
);
2681 spin_unlock_bh(&vscsi
->intr_lock
);
2683 switch (cmd
->type
) {
2685 ibmvscsis_parse_cmd(vscsi
, cmd
);
2687 case TASK_MANAGEMENT
:
2688 ibmvscsis_parse_task(vscsi
, cmd
);
2691 dev_err(&vscsi
->dev
, "scheduler, invalid cmd type %d\n",
2693 spin_lock_bh(&vscsi
->intr_lock
);
2694 ibmvscsis_free_cmd_resources(vscsi
, cmd
);
2695 spin_unlock_bh(&vscsi
->intr_lock
);
2700 static int ibmvscsis_alloc_cmds(struct scsi_info
*vscsi
, int num
)
2702 struct ibmvscsis_cmd
*cmd
;
2705 INIT_LIST_HEAD(&vscsi
->free_cmd
);
2706 vscsi
->cmd_pool
= kcalloc(num
, sizeof(struct ibmvscsis_cmd
),
2708 if (!vscsi
->cmd_pool
)
2711 for (i
= 0, cmd
= (struct ibmvscsis_cmd
*)vscsi
->cmd_pool
; i
< num
;
2713 cmd
->adapter
= vscsi
;
2714 INIT_WORK(&cmd
->work
, ibmvscsis_scheduler
);
2715 list_add_tail(&cmd
->list
, &vscsi
->free_cmd
);
2721 static void ibmvscsis_free_cmds(struct scsi_info
*vscsi
)
2723 kfree(vscsi
->cmd_pool
);
2724 vscsi
->cmd_pool
= NULL
;
2725 INIT_LIST_HEAD(&vscsi
->free_cmd
);
2729 * ibmvscsis_service_wait_q() - Service Waiting Queue
2730 * @timer: Pointer to timer which has expired
2732 * This routine is called when the timer pops to service the waiting
2733 * queue. Elements on the queue have completed, their responses have been
2734 * copied to the client, but the client's response queue was full so
2735 * the queue message could not be sent. The routine grabs the proper locks
2736 * and calls send messages.
2738 * EXECUTION ENVIRONMENT:
2739 * called at interrupt level
2741 static enum hrtimer_restart
ibmvscsis_service_wait_q(struct hrtimer
*timer
)
2743 struct timer_cb
*p_timer
= container_of(timer
, struct timer_cb
, timer
);
2744 struct scsi_info
*vscsi
= container_of(p_timer
, struct scsi_info
,
2747 spin_lock_bh(&vscsi
->intr_lock
);
2748 p_timer
->timer_pops
+= 1;
2749 p_timer
->started
= false;
2750 ibmvscsis_send_messages(vscsi
);
2751 spin_unlock_bh(&vscsi
->intr_lock
);
2753 return HRTIMER_NORESTART
;
2756 static long ibmvscsis_alloctimer(struct scsi_info
*vscsi
)
2758 struct timer_cb
*p_timer
;
2760 p_timer
= &vscsi
->rsp_q_timer
;
2761 hrtimer_init(&p_timer
->timer
, CLOCK_MONOTONIC
, HRTIMER_MODE_REL
);
2763 p_timer
->timer
.function
= ibmvscsis_service_wait_q
;
2764 p_timer
->started
= false;
2765 p_timer
->timer_pops
= 0;
2767 return ADAPT_SUCCESS
;
2770 static void ibmvscsis_freetimer(struct scsi_info
*vscsi
)
2772 struct timer_cb
*p_timer
;
2774 p_timer
= &vscsi
->rsp_q_timer
;
2776 (void)hrtimer_cancel(&p_timer
->timer
);
2778 p_timer
->started
= false;
2779 p_timer
->timer_pops
= 0;
2782 static irqreturn_t
ibmvscsis_interrupt(int dummy
, void *data
)
2784 struct scsi_info
*vscsi
= data
;
2786 vio_disable_interrupts(vscsi
->dma_dev
);
2787 tasklet_schedule(&vscsi
->work_task
);
2793 * ibmvscsis_check_q() - Helper function to Check Init Message Valid
2794 * @vscsi: Pointer to our adapter structure
2796 * Checks if a initialize message was queued by the initiatior
2797 * while the timing window was open. This function is called from
2798 * probe after the CRQ is created and interrupts are enabled.
2799 * It would only be used by adapters who wait for some event before
2800 * completing the init handshake with the client. For ibmvscsi, this
2801 * event is waiting for the port to be enabled.
2803 * EXECUTION ENVIRONMENT:
2804 * Process level only, interrupt lock held
2806 static long ibmvscsis_check_q(struct scsi_info
*vscsi
)
2811 rc
= ibmvscsis_check_init_msg(vscsi
, &format
);
2813 ibmvscsis_post_disconnect(vscsi
, ERR_DISCONNECT_RECONNECT
, 0);
2814 else if (format
== UNUSED_FORMAT
)
2815 vscsi
->state
= WAIT_ENABLED
;
2817 vscsi
->state
= PART_UP_WAIT_ENAB
;
2823 * ibmvscsis_enable_change_state() - Set new state based on enabled status
2824 * @vscsi: Pointer to our adapter structure
2826 * This function determines our new state now that we are enabled. This
2827 * may involve sending an Init Complete message to the client.
2829 * Must be called with interrupt lock held.
2831 static long ibmvscsis_enable_change_state(struct scsi_info
*vscsi
)
2833 long rc
= ADAPT_SUCCESS
;
2835 handle_state_change
:
2836 switch (vscsi
->state
) {
2838 rc
= ibmvscsis_send_init_message(vscsi
, INIT_MSG
);
2843 vscsi
->state
= WAIT_CONNECTION
;
2854 vscsi
->state
= UNDEFINED
;
2859 case PART_UP_WAIT_ENAB
:
2860 rc
= ibmvscsis_send_init_message(vscsi
, INIT_COMPLETE_MSG
);
2863 vscsi
->state
= CONNECTED
;
2869 vscsi
->state
= WAIT_ENABLED
;
2870 goto handle_state_change
;
2884 case WAIT_CONNECTION
:
2886 case SRP_PROCESSING
:
2890 /* should not be able to get here */
2893 vscsi
->state
= UNDEFINED
;
2896 /* driver should never allow this to happen */
2897 case ERR_DISCONNECT
:
2898 case ERR_DISCONNECT_RECONNECT
:
2900 dev_err(&vscsi
->dev
, "in invalid state %d during enable_change_state\n",
2910 * ibmvscsis_create_command_q() - Create Command Queue
2911 * @vscsi: Pointer to our adapter structure
2912 * @num_cmds: Currently unused. In the future, may be used to determine
2913 * the size of the CRQ.
2915 * Allocates memory for command queue maps remote memory into an ioba
2916 * initializes the command response queue
2918 * EXECUTION ENVIRONMENT:
2919 * Process level only
2921 static long ibmvscsis_create_command_q(struct scsi_info
*vscsi
, int num_cmds
)
2925 struct vio_dev
*vdev
= vscsi
->dma_dev
;
2927 /* We might support multiple pages in the future, but just 1 for now */
2930 vscsi
->cmd_q
.size
= pages
;
2932 vscsi
->cmd_q
.base_addr
=
2933 (struct viosrp_crq
*)get_zeroed_page(GFP_KERNEL
);
2934 if (!vscsi
->cmd_q
.base_addr
)
2937 vscsi
->cmd_q
.mask
= ((uint
)pages
* CRQ_PER_PAGE
) - 1;
2939 vscsi
->cmd_q
.crq_token
= dma_map_single(&vdev
->dev
,
2940 vscsi
->cmd_q
.base_addr
,
2941 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
2942 if (dma_mapping_error(&vdev
->dev
, vscsi
->cmd_q
.crq_token
)) {
2943 free_page((unsigned long)vscsi
->cmd_q
.base_addr
);
2947 rc
= h_reg_crq(vscsi
->dds
.unit_id
, vscsi
->cmd_q
.crq_token
, PAGE_SIZE
);
2949 if (rc
== H_CLOSED
) {
2950 vscsi
->state
= WAIT_ENABLED
;
2953 dma_unmap_single(&vdev
->dev
, vscsi
->cmd_q
.crq_token
,
2954 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
2955 free_page((unsigned long)vscsi
->cmd_q
.base_addr
);
2959 vscsi
->state
= WAIT_ENABLED
;
2966 * ibmvscsis_destroy_command_q - Destroy Command Queue
2967 * @vscsi: Pointer to our adapter structure
2969 * Releases memory for command queue and unmaps mapped remote memory.
2971 * EXECUTION ENVIRONMENT:
2972 * Process level only
2974 static void ibmvscsis_destroy_command_q(struct scsi_info
*vscsi
)
2976 dma_unmap_single(&vscsi
->dma_dev
->dev
, vscsi
->cmd_q
.crq_token
,
2977 PAGE_SIZE
, DMA_BIDIRECTIONAL
);
2978 free_page((unsigned long)vscsi
->cmd_q
.base_addr
);
2979 vscsi
->cmd_q
.base_addr
= NULL
;
2980 vscsi
->state
= NO_QUEUE
;
2983 static u8
ibmvscsis_fast_fail(struct scsi_info
*vscsi
,
2984 struct ibmvscsis_cmd
*cmd
)
2986 struct iu_entry
*iue
= cmd
->iue
;
2987 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
2988 struct srp_cmd
*srp
= (struct srp_cmd
*)iue
->sbuf
->buf
;
2989 struct scsi_sense_hdr sshdr
;
2990 u8 rc
= se_cmd
->scsi_status
;
2992 if (vscsi
->fast_fail
&& (READ_CMD(srp
->cdb
) || WRITE_CMD(srp
->cdb
)))
2993 if (scsi_normalize_sense(se_cmd
->sense_buffer
,
2994 se_cmd
->scsi_sense_length
, &sshdr
))
2995 if (sshdr
.sense_key
== HARDWARE_ERROR
&&
2996 (se_cmd
->residual_count
== 0 ||
2997 se_cmd
->residual_count
== se_cmd
->data_length
)) {
2999 cmd
->flags
|= CMD_FAST_FAIL
;
3006 * srp_build_response() - Build an SRP response buffer
3007 * @vscsi: Pointer to our adapter structure
3008 * @cmd: Pointer to command for which to send the response
3009 * @len_p: Where to return the length of the IU response sent. This
3010 * is needed to construct the CRQ response.
3012 * Build the SRP response buffer and copy it to the client's memory space.
3014 static long srp_build_response(struct scsi_info
*vscsi
,
3015 struct ibmvscsis_cmd
*cmd
, uint
*len_p
)
3017 struct iu_entry
*iue
= cmd
->iue
;
3018 struct se_cmd
*se_cmd
= &cmd
->se_cmd
;
3019 struct srp_rsp
*rsp
;
3024 long rc
= ADAPT_SUCCESS
;
3026 spin_lock_bh(&vscsi
->intr_lock
);
3028 rsp
= &vio_iu(iue
)->srp
.rsp
;
3030 memset(rsp
, 0, len
);
3033 rsp
->opcode
= SRP_RSP
;
3035 if (vscsi
->credit
> 0 && vscsi
->state
== SRP_PROCESSING
)
3036 rsp
->req_lim_delta
= cpu_to_be32(vscsi
->credit
);
3038 rsp
->req_lim_delta
= cpu_to_be32(1 + vscsi
->credit
);
3039 rsp
->tag
= cmd
->rsp
.tag
;
3042 if (cmd
->type
== SCSI_CDB
) {
3043 rsp
->status
= ibmvscsis_fast_fail(vscsi
, cmd
);
3045 pr_debug("build_resp: cmd %p, scsi status %d\n", cmd
,
3047 ibmvscsis_determine_resid(se_cmd
, rsp
);
3048 if (se_cmd
->scsi_sense_length
&& se_cmd
->sense_buffer
) {
3049 rsp
->sense_data_len
=
3050 cpu_to_be32(se_cmd
->scsi_sense_length
);
3051 rsp
->flags
|= SRP_RSP_FLAG_SNSVALID
;
3052 len
+= se_cmd
->scsi_sense_length
;
3053 memcpy(data
, se_cmd
->sense_buffer
,
3054 se_cmd
->scsi_sense_length
);
3056 rsp
->sol_not
= (cmd
->rsp
.sol_not
& UCSOLNT
) >>
3058 } else if (cmd
->flags
& CMD_FAST_FAIL
) {
3059 pr_debug("build_resp: cmd %p, fast fail\n", cmd
);
3060 rsp
->sol_not
= (cmd
->rsp
.sol_not
& UCSOLNT
) >>
3063 rsp
->sol_not
= (cmd
->rsp
.sol_not
& SCSOLNT
) >>
3067 /* this is task management */
3069 rsp
->resp_data_len
= cpu_to_be32(4);
3070 rsp
->flags
|= SRP_RSP_FLAG_RSPVALID
;
3072 switch (se_cmd
->se_tmr_req
->response
) {
3073 case TMR_FUNCTION_COMPLETE
:
3074 case TMR_TASK_DOES_NOT_EXIST
:
3075 rsp_code
= SRP_TASK_MANAGEMENT_FUNCTION_COMPLETE
;
3076 rsp
->sol_not
= (cmd
->rsp
.sol_not
& SCSOLNT
) >>
3079 case TMR_TASK_MGMT_FUNCTION_NOT_SUPPORTED
:
3080 case TMR_LUN_DOES_NOT_EXIST
:
3081 rsp_code
= SRP_TASK_MANAGEMENT_FUNCTION_NOT_SUPPORTED
;
3082 rsp
->sol_not
= (cmd
->rsp
.sol_not
& UCSOLNT
) >>
3085 case TMR_FUNCTION_FAILED
:
3086 case TMR_FUNCTION_REJECTED
:
3088 rsp_code
= SRP_TASK_MANAGEMENT_FUNCTION_FAILED
;
3089 rsp
->sol_not
= (cmd
->rsp
.sol_not
& UCSOLNT
) >>
3094 tsk_status
= (u32
*)data
;
3095 *tsk_status
= cpu_to_be32(rsp_code
);
3096 data
= (char *)(tsk_status
+ 1);
3101 rc
= h_copy_rdma(len
, vscsi
->dds
.window
[LOCAL
].liobn
, iue
->sbuf
->dma
,
3102 vscsi
->dds
.window
[REMOTE
].liobn
,
3103 be64_to_cpu(iue
->remote_token
));
3111 if (connection_broken(vscsi
))
3112 vscsi
->flags
|= RESPONSE_Q_DOWN
| CLIENT_FAILED
;
3114 dev_err(&vscsi
->dev
, "build_response: error copying to client, rc %ld, flags 0x%x, state 0x%hx\n",
3115 rc
, vscsi
->flags
, vscsi
->state
);
3120 dev_err(&vscsi
->dev
, "build_response: error copying to client, rc %ld\n",
3125 spin_unlock_bh(&vscsi
->intr_lock
);
3130 static int ibmvscsis_rdma(struct ibmvscsis_cmd
*cmd
, struct scatterlist
*sg
,
3131 int nsg
, struct srp_direct_buf
*md
, int nmd
,
3132 enum dma_data_direction dir
, unsigned int bytes
)
3134 struct iu_entry
*iue
= cmd
->iue
;
3135 struct srp_target
*target
= iue
->target
;
3136 struct scsi_info
*vscsi
= target
->ldata
;
3137 struct scatterlist
*sgp
;
3138 dma_addr_t client_ioba
, server_ioba
;
3140 ulong client_len
, server_len
;
3145 pr_debug("rdma: dir %d, bytes 0x%x\n", dir
, bytes
);
3157 if (client_len
== 0) {
3158 if (md_idx
>= nmd
) {
3159 dev_err(&vscsi
->dev
, "rdma: ran out of client memory descriptors\n");
3163 client_ioba
= be64_to_cpu(md
[md_idx
].va
);
3164 client_len
= be32_to_cpu(md
[md_idx
].len
);
3166 if (server_len
== 0) {
3168 dev_err(&vscsi
->dev
, "rdma: ran out of scatter/gather list\n");
3172 server_ioba
= sg_dma_address(sgp
);
3173 server_len
= sg_dma_len(sgp
);
3178 if (buf_len
> client_len
)
3179 buf_len
= client_len
;
3181 if (buf_len
> server_len
)
3182 buf_len
= server_len
;
3184 if (buf_len
> max_vdma_size
)
3185 buf_len
= max_vdma_size
;
3187 if (dir
== DMA_TO_DEVICE
) {
3188 /* read from client */
3189 rc
= h_copy_rdma(buf_len
,
3190 vscsi
->dds
.window
[REMOTE
].liobn
,
3192 vscsi
->dds
.window
[LOCAL
].liobn
,
3195 /* write to client */
3196 struct srp_cmd
*srp
= (struct srp_cmd
*)iue
->sbuf
->buf
;
3198 if (!READ_CMD(srp
->cdb
))
3199 print_hex_dump_bytes(" data:", DUMP_PREFIX_NONE
,
3200 sg_virt(sgp
), buf_len
);
3201 /* The h_copy_rdma will cause phyp, running in another
3202 * partition, to read memory, so we need to make sure
3203 * the data has been written out, hence these syncs.
3205 /* ensure that everything is in memory */
3207 /* ensure that memory has been made visible */
3209 rc
= h_copy_rdma(buf_len
,
3210 vscsi
->dds
.window
[LOCAL
].liobn
,
3212 vscsi
->dds
.window
[REMOTE
].liobn
,
3221 if (connection_broken(vscsi
)) {
3222 spin_lock_bh(&vscsi
->intr_lock
);
3224 (RESPONSE_Q_DOWN
| CLIENT_FAILED
);
3225 spin_unlock_bh(&vscsi
->intr_lock
);
3227 dev_err(&vscsi
->dev
, "rdma: h_copy_rdma failed, rc %ld\n",
3232 dev_err(&vscsi
->dev
, "rdma: unknown error %ld from h_copy_rdma\n",
3240 client_len
-= buf_len
;
3241 if (client_len
== 0)
3244 client_ioba
+= buf_len
;
3246 server_len
-= buf_len
;
3247 if (server_len
== 0)
3250 server_ioba
+= buf_len
;
3261 * ibmvscsis_handle_crq() - Handle CRQ
3262 * @data: Pointer to our adapter structure
3264 * Read the command elements from the command queue and copy the payloads
3265 * associated with the command elements to local memory and execute the
3268 * Note: this is an edge triggered interrupt. It can not be shared.
3270 static void ibmvscsis_handle_crq(unsigned long data
)
3272 struct scsi_info
*vscsi
= (struct scsi_info
*)data
;
3273 struct viosrp_crq
*crq
;
3278 spin_lock_bh(&vscsi
->intr_lock
);
3280 pr_debug("got interrupt\n");
3283 * if we are in a path where we are waiting for all pending commands
3284 * to complete because we received a transport event and anything in
3285 * the command queue is for a new connection, do nothing
3287 if (TARGET_STOP(vscsi
)) {
3288 vio_enable_interrupts(vscsi
->dma_dev
);
3290 pr_debug("handle_crq, don't process: flags 0x%x, state 0x%hx\n",
3291 vscsi
->flags
, vscsi
->state
);
3292 spin_unlock_bh(&vscsi
->intr_lock
);
3296 rc
= vscsi
->flags
& SCHEDULE_DISCONNECT
;
3297 crq
= vscsi
->cmd_q
.base_addr
+ vscsi
->cmd_q
.index
;
3303 * These are edege triggered interrupts. After dropping out of
3304 * the while loop, the code must check for work since an
3305 * interrupt could be lost, and an elment be left on the queue,
3309 vscsi
->cmd_q
.index
=
3310 (vscsi
->cmd_q
.index
+ 1) & vscsi
->cmd_q
.mask
;
3313 rc
= ibmvscsis_parse_command(vscsi
, crq
);
3315 if ((uint
)crq
->valid
== VALID_TRANS_EVENT
) {
3317 * must service the transport layer events even
3318 * in an error state, dont break out until all
3319 * the consecutive transport events have been
3322 rc
= ibmvscsis_trans_event(vscsi
, crq
);
3323 } else if (vscsi
->flags
& TRANS_EVENT
) {
3325 * if a tranport event has occurred leave
3326 * everything but transport events on the queue
3328 pr_debug("handle_crq, ignoring\n");
3331 * need to decrement the queue index so we can
3332 * look at the elment again
3334 if (vscsi
->cmd_q
.index
)
3335 vscsi
->cmd_q
.index
-= 1;
3338 * index is at 0 it just wrapped.
3339 * have it index last element in q
3341 vscsi
->cmd_q
.index
= vscsi
->cmd_q
.mask
;
3346 crq
->valid
= INVALIDATE_CMD_RESP_EL
;
3348 crq
= vscsi
->cmd_q
.base_addr
+ vscsi
->cmd_q
.index
;
3355 vio_enable_interrupts(vscsi
->dma_dev
);
3357 pr_debug("handle_crq, reenabling interrupts\n");
3364 pr_debug("handle_crq, error: flags 0x%x, state 0x%hx, crq index 0x%x\n",
3365 vscsi
->flags
, vscsi
->state
, vscsi
->cmd_q
.index
);
3368 pr_debug("Leaving handle_crq: schedule_q empty %d, flags 0x%x, state 0x%hx\n",
3369 (int)list_empty(&vscsi
->schedule_q
), vscsi
->flags
,
3372 spin_unlock_bh(&vscsi
->intr_lock
);
3375 static int ibmvscsis_probe(struct vio_dev
*vdev
,
3376 const struct vio_device_id
*id
)
3378 struct scsi_info
*vscsi
;
3383 vscsi
= kzalloc(sizeof(*vscsi
), GFP_KERNEL
);
3386 pr_err("probe: allocation of adapter failed\n");
3390 vscsi
->dma_dev
= vdev
;
3391 vscsi
->dev
= vdev
->dev
;
3392 INIT_LIST_HEAD(&vscsi
->schedule_q
);
3393 INIT_LIST_HEAD(&vscsi
->waiting_rsp
);
3394 INIT_LIST_HEAD(&vscsi
->active_q
);
3396 snprintf(vscsi
->tport
.tport_name
, 256, "%s", dev_name(&vdev
->dev
));
3398 pr_debug("probe tport_name: %s\n", vscsi
->tport
.tport_name
);
3400 rc
= read_dma_window(vscsi
);
3403 pr_debug("Probe: liobn 0x%x, riobn 0x%x\n",
3404 vscsi
->dds
.window
[LOCAL
].liobn
,
3405 vscsi
->dds
.window
[REMOTE
].liobn
);
3407 strcpy(vscsi
->eye
, "VSCSI ");
3408 strncat(vscsi
->eye
, vdev
->name
, MAX_EYE
);
3410 vscsi
->dds
.unit_id
= vdev
->unit_address
;
3412 spin_lock_bh(&ibmvscsis_dev_lock
);
3413 list_add_tail(&vscsi
->list
, &ibmvscsis_dev_list
);
3414 spin_unlock_bh(&ibmvscsis_dev_lock
);
3417 * TBD: How do we determine # of cmds to request? Do we know how
3418 * many "children" we have?
3420 vscsi
->request_limit
= INITIAL_SRP_LIMIT
;
3421 rc
= srp_target_alloc(&vscsi
->target
, &vdev
->dev
, vscsi
->request_limit
,
3426 vscsi
->target
.ldata
= vscsi
;
3428 rc
= ibmvscsis_alloc_cmds(vscsi
, vscsi
->request_limit
);
3430 dev_err(&vscsi
->dev
, "alloc_cmds failed, rc %d, num %d\n",
3431 rc
, vscsi
->request_limit
);
3436 * Note: the lock is used in freeing timers, so must initialize
3437 * first so that ordering in case of error is correct.
3439 spin_lock_init(&vscsi
->intr_lock
);
3441 rc
= ibmvscsis_alloctimer(vscsi
);
3443 dev_err(&vscsi
->dev
, "probe: alloctimer failed, rc %d\n", rc
);
3447 rc
= ibmvscsis_create_command_q(vscsi
, 256);
3449 dev_err(&vscsi
->dev
, "probe: create_command_q failed, rc %d\n",
3454 vscsi
->map_buf
= kzalloc(PAGE_SIZE
, GFP_KERNEL
);
3455 if (!vscsi
->map_buf
) {
3457 dev_err(&vscsi
->dev
, "probe: allocating cmd buffer failed\n");
3461 vscsi
->map_ioba
= dma_map_single(&vdev
->dev
, vscsi
->map_buf
, PAGE_SIZE
,
3463 if (dma_mapping_error(&vdev
->dev
, vscsi
->map_ioba
)) {
3464 dev_err(&vscsi
->dev
, "probe: error mapping command buffer\n");
3468 hrc
= h_vioctl(vscsi
->dds
.unit_id
, H_GET_PARTNER_INFO
,
3469 (u64
)vscsi
->map_ioba
| ((u64
)PAGE_SIZE
<< 32), 0, 0, 0,
3471 if (hrc
== H_SUCCESS
)
3472 vscsi
->client_data
.partition_number
=
3473 be64_to_cpu(*(u64
*)vscsi
->map_buf
);
3475 * We expect the VIOCTL to fail if we're configured as "any
3476 * client can connect" and the client isn't activated yet.
3477 * We'll make the call again when he sends an init msg.
3479 pr_debug("probe hrc %ld, client partition num %d\n",
3480 hrc
, vscsi
->client_data
.partition_number
);
3482 tasklet_init(&vscsi
->work_task
, ibmvscsis_handle_crq
,
3483 (unsigned long)vscsi
);
3485 init_completion(&vscsi
->wait_idle
);
3487 snprintf(wq_name
, 24, "ibmvscsis%s", dev_name(&vdev
->dev
));
3488 vscsi
->work_q
= create_workqueue(wq_name
);
3489 if (!vscsi
->work_q
) {
3491 dev_err(&vscsi
->dev
, "create_workqueue failed\n");
3495 rc
= request_irq(vdev
->irq
, ibmvscsis_interrupt
, 0, "ibmvscsis", vscsi
);
3498 dev_err(&vscsi
->dev
, "probe: request_irq failed, rc %d\n", rc
);
3502 spin_lock_bh(&vscsi
->intr_lock
);
3503 vio_enable_interrupts(vdev
);
3505 dev_err(&vscsi
->dev
, "enabling interrupts failed, rc %d\n", rc
);
3507 spin_unlock_bh(&vscsi
->intr_lock
);
3511 if (ibmvscsis_check_q(vscsi
)) {
3513 dev_err(&vscsi
->dev
, "probe: check_q failed, rc %d\n", rc
);
3514 spin_unlock_bh(&vscsi
->intr_lock
);
3515 goto disable_interrupt
;
3517 spin_unlock_bh(&vscsi
->intr_lock
);
3519 dev_set_drvdata(&vdev
->dev
, vscsi
);
3524 vio_disable_interrupts(vdev
);
3526 free_irq(vdev
->irq
, vscsi
);
3528 destroy_workqueue(vscsi
->work_q
);
3530 dma_unmap_single(&vdev
->dev
, vscsi
->map_ioba
, PAGE_SIZE
,
3533 kfree(vscsi
->map_buf
);
3535 tasklet_kill(&vscsi
->work_task
);
3536 ibmvscsis_unregister_command_q(vscsi
);
3537 ibmvscsis_destroy_command_q(vscsi
);
3539 ibmvscsis_freetimer(vscsi
);
3541 ibmvscsis_free_cmds(vscsi
);
3543 srp_target_free(&vscsi
->target
);
3545 spin_lock_bh(&ibmvscsis_dev_lock
);
3546 list_del(&vscsi
->list
);
3547 spin_unlock_bh(&ibmvscsis_dev_lock
);
3554 static int ibmvscsis_remove(struct vio_dev
*vdev
)
3556 struct scsi_info
*vscsi
= dev_get_drvdata(&vdev
->dev
);
3558 pr_debug("remove (%s)\n", dev_name(&vscsi
->dma_dev
->dev
));
3561 * TBD: Need to handle if there are commands on the waiting_rsp q
3562 * Actually, can there still be cmds outstanding to tcm?
3565 vio_disable_interrupts(vdev
);
3566 free_irq(vdev
->irq
, vscsi
);
3567 destroy_workqueue(vscsi
->work_q
);
3568 dma_unmap_single(&vdev
->dev
, vscsi
->map_ioba
, PAGE_SIZE
,
3570 kfree(vscsi
->map_buf
);
3571 tasklet_kill(&vscsi
->work_task
);
3572 ibmvscsis_unregister_command_q(vscsi
);
3573 ibmvscsis_destroy_command_q(vscsi
);
3574 ibmvscsis_freetimer(vscsi
);
3575 ibmvscsis_free_cmds(vscsi
);
3576 srp_target_free(&vscsi
->target
);
3577 spin_lock_bh(&ibmvscsis_dev_lock
);
3578 list_del(&vscsi
->list
);
3579 spin_unlock_bh(&ibmvscsis_dev_lock
);
3585 static ssize_t
system_id_show(struct device
*dev
,
3586 struct device_attribute
*attr
, char *buf
)
3588 return snprintf(buf
, PAGE_SIZE
, "%s\n", system_id
);
3591 static ssize_t
partition_number_show(struct device
*dev
,
3592 struct device_attribute
*attr
, char *buf
)
3594 return snprintf(buf
, PAGE_SIZE
, "%x\n", partition_number
);
3597 static ssize_t
unit_address_show(struct device
*dev
,
3598 struct device_attribute
*attr
, char *buf
)
3600 struct scsi_info
*vscsi
= container_of(dev
, struct scsi_info
, dev
);
3602 return snprintf(buf
, PAGE_SIZE
, "%x\n", vscsi
->dma_dev
->unit_address
);
3605 static int ibmvscsis_get_system_info(void)
3607 struct device_node
*rootdn
, *vdevdn
;
3608 const char *id
, *model
, *name
;
3611 rootdn
= of_find_node_by_path("/");
3615 model
= of_get_property(rootdn
, "model", NULL
);
3616 id
= of_get_property(rootdn
, "system-id", NULL
);
3618 snprintf(system_id
, sizeof(system_id
), "%s-%s", model
, id
);
3620 name
= of_get_property(rootdn
, "ibm,partition-name", NULL
);
3622 strncpy(partition_name
, name
, sizeof(partition_name
));
3624 num
= of_get_property(rootdn
, "ibm,partition-no", NULL
);
3626 partition_number
= *num
;
3628 of_node_put(rootdn
);
3630 vdevdn
= of_find_node_by_path("/vdevice");
3634 mvds
= of_get_property(vdevdn
, "ibm,max-virtual-dma-size",
3637 max_vdma_size
= *mvds
;
3638 of_node_put(vdevdn
);
3644 static char *ibmvscsis_get_fabric_name(void)
3649 static char *ibmvscsis_get_fabric_wwn(struct se_portal_group
*se_tpg
)
3651 struct ibmvscsis_tport
*tport
=
3652 container_of(se_tpg
, struct ibmvscsis_tport
, se_tpg
);
3654 return tport
->tport_name
;
3657 static u16
ibmvscsis_get_tag(struct se_portal_group
*se_tpg
)
3659 struct ibmvscsis_tport
*tport
=
3660 container_of(se_tpg
, struct ibmvscsis_tport
, se_tpg
);
3662 return tport
->tport_tpgt
;
3665 static u32
ibmvscsis_get_default_depth(struct se_portal_group
*se_tpg
)
3670 static int ibmvscsis_check_true(struct se_portal_group
*se_tpg
)
3675 static int ibmvscsis_check_false(struct se_portal_group
*se_tpg
)
3680 static u32
ibmvscsis_tpg_get_inst_index(struct se_portal_group
*se_tpg
)
3685 static int ibmvscsis_check_stop_free(struct se_cmd
*se_cmd
)
3687 return target_put_sess_cmd(se_cmd
);
3690 static void ibmvscsis_release_cmd(struct se_cmd
*se_cmd
)
3692 struct ibmvscsis_cmd
*cmd
= container_of(se_cmd
, struct ibmvscsis_cmd
,
3694 struct scsi_info
*vscsi
= cmd
->adapter
;
3696 pr_debug("release_cmd %p, flags %d\n", se_cmd
, cmd
->flags
);
3698 spin_lock_bh(&vscsi
->intr_lock
);
3699 /* Remove from active_q */
3700 list_del(&cmd
->list
);
3701 list_add_tail(&cmd
->list
, &vscsi
->waiting_rsp
);
3702 ibmvscsis_send_messages(vscsi
);
3703 spin_unlock_bh(&vscsi
->intr_lock
);
3706 static u32
ibmvscsis_sess_get_index(struct se_session
*se_sess
)
3711 static int ibmvscsis_write_pending(struct se_cmd
*se_cmd
)
3713 struct ibmvscsis_cmd
*cmd
= container_of(se_cmd
, struct ibmvscsis_cmd
,
3715 struct iu_entry
*iue
= cmd
->iue
;
3718 pr_debug("write_pending, se_cmd %p, length 0x%x\n",
3719 se_cmd
, se_cmd
->data_length
);
3721 rc
= srp_transfer_data(cmd
, &vio_iu(iue
)->srp
.cmd
, ibmvscsis_rdma
,
3724 pr_err("srp_transfer_data() failed: %d\n", rc
);
3728 * We now tell TCM to add this WRITE CDB directly into the TCM storage
3729 * object execution queue.
3731 target_execute_cmd(se_cmd
);
3735 static int ibmvscsis_write_pending_status(struct se_cmd
*se_cmd
)
3740 static void ibmvscsis_set_default_node_attrs(struct se_node_acl
*nacl
)
3744 static int ibmvscsis_get_cmd_state(struct se_cmd
*se_cmd
)
3749 static int ibmvscsis_queue_data_in(struct se_cmd
*se_cmd
)
3751 struct ibmvscsis_cmd
*cmd
= container_of(se_cmd
, struct ibmvscsis_cmd
,
3753 struct iu_entry
*iue
= cmd
->iue
;
3754 struct scsi_info
*vscsi
= cmd
->adapter
;
3759 pr_debug("queue_data_in, se_cmd %p, length 0x%x\n",
3760 se_cmd
, se_cmd
->data_length
);
3762 rc
= srp_transfer_data(cmd
, &vio_iu(iue
)->srp
.cmd
, ibmvscsis_rdma
, 1,
3765 pr_err("srp_transfer_data failed: %d\n", rc
);
3766 sd
= se_cmd
->sense_buffer
;
3767 se_cmd
->scsi_sense_length
= 18;
3768 memset(se_cmd
->sense_buffer
, 0, se_cmd
->scsi_sense_length
);
3769 /* Logical Unit Communication Time-out asc/ascq = 0x0801 */
3770 scsi_build_sense_buffer(0, se_cmd
->sense_buffer
, MEDIUM_ERROR
,
3774 srp_build_response(vscsi
, cmd
, &len
);
3775 cmd
->rsp
.format
= SRP_FORMAT
;
3781 static int ibmvscsis_queue_status(struct se_cmd
*se_cmd
)
3783 struct ibmvscsis_cmd
*cmd
= container_of(se_cmd
, struct ibmvscsis_cmd
,
3785 struct scsi_info
*vscsi
= cmd
->adapter
;
3788 pr_debug("queue_status %p\n", se_cmd
);
3790 srp_build_response(vscsi
, cmd
, &len
);
3791 cmd
->rsp
.format
= SRP_FORMAT
;
3797 static void ibmvscsis_queue_tm_rsp(struct se_cmd
*se_cmd
)
3799 struct ibmvscsis_cmd
*cmd
= container_of(se_cmd
, struct ibmvscsis_cmd
,
3801 struct scsi_info
*vscsi
= cmd
->adapter
;
3804 pr_debug("queue_tm_rsp %p, status %d\n",
3805 se_cmd
, (int)se_cmd
->se_tmr_req
->response
);
3807 srp_build_response(vscsi
, cmd
, &len
);
3808 cmd
->rsp
.format
= SRP_FORMAT
;
3812 static void ibmvscsis_aborted_task(struct se_cmd
*se_cmd
)
3814 /* TBD: What (if anything) should we do here? */
3815 pr_debug("ibmvscsis_aborted_task %p\n", se_cmd
);
3818 static struct se_wwn
*ibmvscsis_make_tport(struct target_fabric_configfs
*tf
,
3819 struct config_group
*group
,
3822 struct ibmvscsis_tport
*tport
;
3824 tport
= ibmvscsis_lookup_port(name
);
3826 tport
->tport_proto_id
= SCSI_PROTOCOL_SRP
;
3827 pr_debug("make_tport(%s), pointer:%p, tport_id:%x\n",
3828 name
, tport
, tport
->tport_proto_id
);
3829 return &tport
->tport_wwn
;
3832 return ERR_PTR(-EINVAL
);
3835 static void ibmvscsis_drop_tport(struct se_wwn
*wwn
)
3837 struct ibmvscsis_tport
*tport
= container_of(wwn
,
3838 struct ibmvscsis_tport
,
3841 pr_debug("drop_tport(%s)\n",
3842 config_item_name(&tport
->tport_wwn
.wwn_group
.cg_item
));
3845 static struct se_portal_group
*ibmvscsis_make_tpg(struct se_wwn
*wwn
,
3846 struct config_group
*group
,
3849 struct ibmvscsis_tport
*tport
=
3850 container_of(wwn
, struct ibmvscsis_tport
, tport_wwn
);
3853 tport
->releasing
= false;
3855 rc
= core_tpg_register(&tport
->tport_wwn
, &tport
->se_tpg
,
3856 tport
->tport_proto_id
);
3860 return &tport
->se_tpg
;
3863 static void ibmvscsis_drop_tpg(struct se_portal_group
*se_tpg
)
3865 struct ibmvscsis_tport
*tport
= container_of(se_tpg
,
3866 struct ibmvscsis_tport
,
3869 tport
->releasing
= true;
3870 tport
->enabled
= false;
3873 * Release the virtual I_T Nexus for this ibmvscsis TPG
3875 ibmvscsis_drop_nexus(tport
);
3877 * Deregister the se_tpg from TCM..
3879 core_tpg_deregister(se_tpg
);
3882 static ssize_t
ibmvscsis_wwn_version_show(struct config_item
*item
,
3885 return scnprintf(page
, PAGE_SIZE
, "%s\n", IBMVSCSIS_VERSION
);
3887 CONFIGFS_ATTR_RO(ibmvscsis_wwn_
, version
);
3889 static struct configfs_attribute
*ibmvscsis_wwn_attrs
[] = {
3890 &ibmvscsis_wwn_attr_version
,
3894 static ssize_t
ibmvscsis_tpg_enable_show(struct config_item
*item
,
3897 struct se_portal_group
*se_tpg
= to_tpg(item
);
3898 struct ibmvscsis_tport
*tport
= container_of(se_tpg
,
3899 struct ibmvscsis_tport
,
3902 return snprintf(page
, PAGE_SIZE
, "%d\n", (tport
->enabled
) ? 1 : 0);
3905 static ssize_t
ibmvscsis_tpg_enable_store(struct config_item
*item
,
3906 const char *page
, size_t count
)
3908 struct se_portal_group
*se_tpg
= to_tpg(item
);
3909 struct ibmvscsis_tport
*tport
= container_of(se_tpg
,
3910 struct ibmvscsis_tport
,
3912 struct scsi_info
*vscsi
= container_of(tport
, struct scsi_info
, tport
);
3917 rc
= kstrtoul(page
, 0, &tmp
);
3919 pr_err("Unable to extract srpt_tpg_store_enable\n");
3923 if ((tmp
!= 0) && (tmp
!= 1)) {
3924 pr_err("Illegal value for srpt_tpg_store_enable\n");
3929 tport
->enabled
= true;
3930 spin_lock_bh(&vscsi
->intr_lock
);
3931 lrc
= ibmvscsis_enable_change_state(vscsi
);
3933 pr_err("enable_change_state failed, rc %ld state %d\n",
3935 spin_unlock_bh(&vscsi
->intr_lock
);
3937 tport
->enabled
= false;
3940 pr_debug("tpg_enable_store, state %d\n", vscsi
->state
);
3944 CONFIGFS_ATTR(ibmvscsis_tpg_
, enable
);
3946 static struct configfs_attribute
*ibmvscsis_tpg_attrs
[] = {
3947 &ibmvscsis_tpg_attr_enable
,
3951 static const struct target_core_fabric_ops ibmvscsis_ops
= {
3952 .module
= THIS_MODULE
,
3953 .name
= "ibmvscsis",
3954 .get_fabric_name
= ibmvscsis_get_fabric_name
,
3955 .tpg_get_wwn
= ibmvscsis_get_fabric_wwn
,
3956 .tpg_get_tag
= ibmvscsis_get_tag
,
3957 .tpg_get_default_depth
= ibmvscsis_get_default_depth
,
3958 .tpg_check_demo_mode
= ibmvscsis_check_true
,
3959 .tpg_check_demo_mode_cache
= ibmvscsis_check_true
,
3960 .tpg_check_demo_mode_write_protect
= ibmvscsis_check_false
,
3961 .tpg_check_prod_mode_write_protect
= ibmvscsis_check_false
,
3962 .tpg_get_inst_index
= ibmvscsis_tpg_get_inst_index
,
3963 .check_stop_free
= ibmvscsis_check_stop_free
,
3964 .release_cmd
= ibmvscsis_release_cmd
,
3965 .sess_get_index
= ibmvscsis_sess_get_index
,
3966 .write_pending
= ibmvscsis_write_pending
,
3967 .write_pending_status
= ibmvscsis_write_pending_status
,
3968 .set_default_node_attributes
= ibmvscsis_set_default_node_attrs
,
3969 .get_cmd_state
= ibmvscsis_get_cmd_state
,
3970 .queue_data_in
= ibmvscsis_queue_data_in
,
3971 .queue_status
= ibmvscsis_queue_status
,
3972 .queue_tm_rsp
= ibmvscsis_queue_tm_rsp
,
3973 .aborted_task
= ibmvscsis_aborted_task
,
3975 * Setup function pointers for logic in target_core_fabric_configfs.c
3977 .fabric_make_wwn
= ibmvscsis_make_tport
,
3978 .fabric_drop_wwn
= ibmvscsis_drop_tport
,
3979 .fabric_make_tpg
= ibmvscsis_make_tpg
,
3980 .fabric_drop_tpg
= ibmvscsis_drop_tpg
,
3982 .tfc_wwn_attrs
= ibmvscsis_wwn_attrs
,
3983 .tfc_tpg_base_attrs
= ibmvscsis_tpg_attrs
,
3986 static void ibmvscsis_dev_release(struct device
*dev
) {};
3988 static struct class_attribute ibmvscsis_class_attrs
[] = {
3992 static struct device_attribute dev_attr_system_id
=
3993 __ATTR(system_id
, S_IRUGO
, system_id_show
, NULL
);
3995 static struct device_attribute dev_attr_partition_number
=
3996 __ATTR(partition_number
, S_IRUGO
, partition_number_show
, NULL
);
3998 static struct device_attribute dev_attr_unit_address
=
3999 __ATTR(unit_address
, S_IRUGO
, unit_address_show
, NULL
);
4001 static struct attribute
*ibmvscsis_dev_attrs
[] = {
4002 &dev_attr_system_id
.attr
,
4003 &dev_attr_partition_number
.attr
,
4004 &dev_attr_unit_address
.attr
,
4006 ATTRIBUTE_GROUPS(ibmvscsis_dev
);
4008 static struct class ibmvscsis_class
= {
4009 .name
= "ibmvscsis",
4010 .dev_release
= ibmvscsis_dev_release
,
4011 .class_attrs
= ibmvscsis_class_attrs
,
4012 .dev_groups
= ibmvscsis_dev_groups
,
4015 static struct vio_device_id ibmvscsis_device_table
[] = {
4016 { "v-scsi-host", "IBM,v-scsi-host" },
4019 MODULE_DEVICE_TABLE(vio
, ibmvscsis_device_table
);
4021 static struct vio_driver ibmvscsis_driver
= {
4022 .name
= "ibmvscsis",
4023 .id_table
= ibmvscsis_device_table
,
4024 .probe
= ibmvscsis_probe
,
4025 .remove
= ibmvscsis_remove
,
4029 * ibmvscsis_init() - Kernel Module initialization
4031 * Note: vio_register_driver() registers callback functions, and at least one
4032 * of those callback functions calls TCM - Linux IO Target Subsystem, thus
4033 * the SCSI Target template must be registered before vio_register_driver()
4036 static int __init
ibmvscsis_init(void)
4040 rc
= ibmvscsis_get_system_info();
4042 pr_err("rc %d from get_system_info\n", rc
);
4046 rc
= class_register(&ibmvscsis_class
);
4048 pr_err("failed class register\n");
4052 rc
= target_register_template(&ibmvscsis_ops
);
4054 pr_err("rc %d from target_register_template\n", rc
);
4055 goto unregister_class
;
4058 rc
= vio_register_driver(&ibmvscsis_driver
);
4060 pr_err("rc %d from vio_register_driver\n", rc
);
4061 goto unregister_target
;
4067 target_unregister_template(&ibmvscsis_ops
);
4069 class_unregister(&ibmvscsis_class
);
4074 static void __exit
ibmvscsis_exit(void)
4076 pr_info("Unregister IBM virtual SCSI host driver\n");
4077 vio_unregister_driver(&ibmvscsis_driver
);
4078 target_unregister_template(&ibmvscsis_ops
);
4079 class_unregister(&ibmvscsis_class
);
4082 MODULE_DESCRIPTION("IBMVSCSIS fabric driver");
4083 MODULE_AUTHOR("Bryant G. Ly and Michael Cyr");
4084 MODULE_LICENSE("GPL");
4085 MODULE_VERSION(IBMVSCSIS_VERSION
);
4086 module_init(ibmvscsis_init
);
4087 module_exit(ibmvscsis_exit
);