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