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