Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  *   Copyright (C) 2017, Microsoft Corporation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *   Author(s): Long Li <longli@microsoft.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8) #include <linux/highmem.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9) #include "smbdirect.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10) #include "cifs_debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11) #include "cifsproto.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12) #include "smb2proto.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) static struct smbd_response *get_empty_queue_buffer(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15) 		struct smbd_connection *info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) static struct smbd_response *get_receive_buffer(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) 		struct smbd_connection *info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) static void put_receive_buffer(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) 		struct smbd_connection *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) 		struct smbd_response *response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) static int allocate_receive_buffers(struct smbd_connection *info, int num_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) static void destroy_receive_buffers(struct smbd_connection *info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) static void put_empty_packet(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) 		struct smbd_connection *info, struct smbd_response *response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) static void enqueue_reassembly(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 		struct smbd_connection *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) 		struct smbd_response *response, int data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) static struct smbd_response *_get_first_reassembly(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) 		struct smbd_connection *info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) static int smbd_post_recv(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) 		struct smbd_connection *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 		struct smbd_response *response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) static int smbd_post_send_empty(struct smbd_connection *info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) static int smbd_post_send_data(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) 		struct smbd_connection *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 		struct kvec *iov, int n_vec, int remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) static int smbd_post_send_page(struct smbd_connection *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 		struct page *page, unsigned long offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) 		size_t size, int remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) static void destroy_mr_list(struct smbd_connection *info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) static int allocate_mr_list(struct smbd_connection *info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) /* SMBD version number */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) #define SMBD_V1	0x0100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) /* Port numbers for SMBD transport */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) #define SMB_PORT	445
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) #define SMBD_PORT	5445
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) /* Address lookup and resolve timeout in ms */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) #define RDMA_RESOLVE_TIMEOUT	5000
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) /* SMBD negotiation timeout in seconds */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) #define SMBD_NEGOTIATE_TIMEOUT	120
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) /* SMBD minimum receive size and fragmented sized defined in [MS-SMBD] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) #define SMBD_MIN_RECEIVE_SIZE		128
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) #define SMBD_MIN_FRAGMENTED_SIZE	131072
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65)  * Default maximum number of RDMA read/write outstanding on this connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66)  * This value is possibly decreased during QP creation on hardware limit
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) #define SMBD_CM_RESPONDER_RESOURCES	32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) /* Maximum number of retries on data transfer operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) #define SMBD_CM_RETRY			6
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) /* No need to retry on Receiver Not Ready since SMBD manages credits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) #define SMBD_CM_RNR_RETRY		0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76)  * User configurable initial values per SMBD transport connection
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77)  * as defined in [MS-SMBD] 3.1.1.1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78)  * Those may change after a SMBD negotiation
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) /* The local peer's maximum number of credits to grant to the peer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) int smbd_receive_credit_max = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) /* The remote peer's credit request of local peer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) int smbd_send_credit_target = 255;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) /* The maximum single message size can be sent to remote peer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) int smbd_max_send_size = 1364;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) /*  The maximum fragmented upper-layer payload receive size supported */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) int smbd_max_fragmented_recv_size = 1024 * 1024;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) /*  The maximum single-message size which can be received */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) int smbd_max_receive_size = 8192;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) /* The timeout to initiate send of a keepalive message on idle */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) int smbd_keep_alive_interval = 120;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  * User configurable initial values for RDMA transport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100)  * The actual values used may be lower and are limited to hardware capabilities
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) /* Default maximum number of SGEs in a RDMA write/read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) int smbd_max_frmr_depth = 2048;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) /* If payload is less than this byte, use RDMA send/recv not read/write */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) int rdma_readwrite_threshold = 4096;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) /* Transport logging functions
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109)  * Logging are defined as classes. They can be OR'ed to define the actual
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110)  * logging level via module parameter smbd_logging_class
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111)  * e.g. cifs.smbd_logging_class=0xa0 will log all log_rdma_recv() and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112)  * log_rdma_event()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) #define LOG_OUTGOING			0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) #define LOG_INCOMING			0x2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) #define LOG_READ			0x4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #define LOG_WRITE			0x8
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) #define LOG_RDMA_SEND			0x10
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) #define LOG_RDMA_RECV			0x20
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) #define LOG_KEEP_ALIVE			0x40
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) #define LOG_RDMA_EVENT			0x80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) #define LOG_RDMA_MR			0x100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) static unsigned int smbd_logging_class;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) module_param(smbd_logging_class, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) MODULE_PARM_DESC(smbd_logging_class,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) 	"Logging class for SMBD transport 0x0 to 0x100");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) #define ERR		0x0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129) #define INFO		0x1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130) static unsigned int smbd_logging_level = ERR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) module_param(smbd_logging_level, uint, 0644);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) MODULE_PARM_DESC(smbd_logging_level,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) 	"Logging level for SMBD transport, 0 (default): error, 1: info");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) #define log_rdma(level, class, fmt, args...)				\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) do {									\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	if (level <= smbd_logging_level || class & smbd_logging_class)	\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 		cifs_dbg(VFS, "%s:%d " fmt, __func__, __LINE__, ##args);\
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) } while (0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) #define log_outgoing(level, fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 		log_rdma(level, LOG_OUTGOING, fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) #define log_incoming(level, fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) 		log_rdma(level, LOG_INCOMING, fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) #define log_read(level, fmt, args...)	log_rdma(level, LOG_READ, fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) #define log_write(level, fmt, args...)	log_rdma(level, LOG_WRITE, fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) #define log_rdma_send(level, fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) 		log_rdma(level, LOG_RDMA_SEND, fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) #define log_rdma_recv(level, fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 		log_rdma(level, LOG_RDMA_RECV, fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) #define log_keep_alive(level, fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 		log_rdma(level, LOG_KEEP_ALIVE, fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) #define log_rdma_event(level, fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		log_rdma(level, LOG_RDMA_EVENT, fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) #define log_rdma_mr(level, fmt, args...) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 		log_rdma(level, LOG_RDMA_MR, fmt, ##args)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) static void smbd_disconnect_rdma_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160) 	struct smbd_connection *info =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161) 		container_of(work, struct smbd_connection, disconnect_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163) 	if (info->transport_status == SMBD_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 		info->transport_status = SMBD_DISCONNECTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) 		rdma_disconnect(info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) static void smbd_disconnect_rdma_connection(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171) 	queue_work(info->workqueue, &info->disconnect_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) /* Upcall from RDMA CM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) static int smbd_conn_upcall(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 		struct rdma_cm_id *id, struct rdma_cm_event *event)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 	struct smbd_connection *info = id->context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180) 	log_rdma_event(INFO, "event=%d status=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181) 		event->event, event->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 	switch (event->event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	case RDMA_CM_EVENT_ADDR_RESOLVED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 	case RDMA_CM_EVENT_ROUTE_RESOLVED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) 		info->ri_rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187) 		complete(&info->ri_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	case RDMA_CM_EVENT_ADDR_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		info->ri_rc = -EHOSTUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		complete(&info->ri_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	case RDMA_CM_EVENT_ROUTE_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 		info->ri_rc = -ENETUNREACH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) 		complete(&info->ri_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200) 	case RDMA_CM_EVENT_ESTABLISHED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201) 		log_rdma_event(INFO, "connected event=%d\n", event->event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 		info->transport_status = SMBD_CONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) 		wake_up_interruptible(&info->conn_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) 	case RDMA_CM_EVENT_CONNECT_ERROR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	case RDMA_CM_EVENT_UNREACHABLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	case RDMA_CM_EVENT_REJECTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 		log_rdma_event(INFO, "connecting failed event=%d\n", event->event);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 		info->transport_status = SMBD_DISCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 		wake_up_interruptible(&info->conn_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	case RDMA_CM_EVENT_DEVICE_REMOVAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 	case RDMA_CM_EVENT_DISCONNECTED:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		/* This happenes when we fail the negotiation */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 		if (info->transport_status == SMBD_NEGOTIATE_FAILED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 			info->transport_status = SMBD_DISCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 			wake_up(&info->conn_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 		info->transport_status = SMBD_DISCONNECTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 		wake_up_interruptible(&info->disconn_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 		wake_up_interruptible(&info->wait_reassembly_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 		wake_up_interruptible_all(&info->wait_send_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) /* Upcall from RDMA QP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) smbd_qp_async_error_upcall(struct ib_event *event, void *context)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 	struct smbd_connection *info = context;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 	log_rdma_event(ERR, "%s on device %s info %p\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 		ib_event_msg(event->event), event->device->name, info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 	switch (event->event) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 	case IB_EVENT_CQ_ERR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	case IB_EVENT_QP_FATAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) 		smbd_disconnect_rdma_connection(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) static inline void *smbd_request_payload(struct smbd_request *request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	return (void *)request->packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) static inline void *smbd_response_payload(struct smbd_response *response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 	return (void *)response->packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) /* Called when a RDMA send is done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) static void send_done(struct ib_cq *cq, struct ib_wc *wc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 	struct smbd_request *request =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		container_of(wc->wr_cqe, struct smbd_request, cqe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 	log_rdma_send(INFO, "smbd_request %p completed wc->status=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		request, wc->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_SEND) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 		log_rdma_send(ERR, "wc->status=%d wc->opcode=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 			wc->status, wc->opcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		smbd_disconnect_rdma_connection(request->info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 	for (i = 0; i < request->num_sge; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 		ib_dma_unmap_single(request->info->id->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 			request->sge[i].addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 			request->sge[i].length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 			DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	if (atomic_dec_and_test(&request->info->send_pending))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 		wake_up(&request->info->wait_send_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) 	wake_up(&request->info->wait_post_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) 	mempool_free(request, request->info->request_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) static void dump_smbd_negotiate_resp(struct smbd_negotiate_resp *resp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	log_rdma_event(INFO, "resp message min_version %u max_version %u negotiated_version %u credits_requested %u credits_granted %u status %u max_readwrite_size %u preferred_send_size %u max_receive_size %u max_fragmented_size %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 		       resp->min_version, resp->max_version,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		       resp->negotiated_version, resp->credits_requested,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 		       resp->credits_granted, resp->status,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 		       resp->max_readwrite_size, resp->preferred_send_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 		       resp->max_receive_size, resp->max_fragmented_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306)  * Process a negotiation response message, according to [MS-SMBD]3.1.5.7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307)  * response, packet_length: the negotiation response message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308)  * return value: true if negotiation is a success, false if failed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) static bool process_negotiation_response(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 		struct smbd_response *response, int packet_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 	struct smbd_connection *info = response->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) 	struct smbd_negotiate_resp *packet = smbd_response_payload(response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316) 	if (packet_length < sizeof(struct smbd_negotiate_resp)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 		log_rdma_event(ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) 			"error: packet_length=%d\n", packet_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) 	if (le16_to_cpu(packet->negotiated_version) != SMBD_V1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 		log_rdma_event(ERR, "error: negotiated_version=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 			le16_to_cpu(packet->negotiated_version));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) 	info->protocol = le16_to_cpu(packet->negotiated_version);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) 	if (packet->credits_requested == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) 		log_rdma_event(ERR, "error: credits_requested==0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) 	info->receive_credit_target = le16_to_cpu(packet->credits_requested);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) 	if (packet->credits_granted == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336) 		log_rdma_event(ERR, "error: credits_granted==0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339) 	atomic_set(&info->send_credits, le16_to_cpu(packet->credits_granted));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) 	atomic_set(&info->receive_credits, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	if (le32_to_cpu(packet->preferred_send_size) > info->max_receive_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 		log_rdma_event(ERR, "error: preferred_send_size=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 			le32_to_cpu(packet->preferred_send_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 	info->max_receive_size = le32_to_cpu(packet->preferred_send_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 	if (le32_to_cpu(packet->max_receive_size) < SMBD_MIN_RECEIVE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		log_rdma_event(ERR, "error: max_receive_size=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 			le32_to_cpu(packet->max_receive_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	info->max_send_size = min_t(int, info->max_send_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) 					le32_to_cpu(packet->max_receive_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) 	if (le32_to_cpu(packet->max_fragmented_size) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) 			SMBD_MIN_FRAGMENTED_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 		log_rdma_event(ERR, "error: max_fragmented_size=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 			le32_to_cpu(packet->max_fragmented_size));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	info->max_fragmented_send_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 		le32_to_cpu(packet->max_fragmented_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	info->rdma_readwrite_threshold =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 		rdma_readwrite_threshold > info->max_fragmented_send_size ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 		info->max_fragmented_send_size :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 		rdma_readwrite_threshold;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 	info->max_readwrite_size = min_t(u32,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 			le32_to_cpu(packet->max_readwrite_size),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 			info->max_frmr_depth * PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 	info->max_frmr_depth = info->max_readwrite_size / PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) static void smbd_post_send_credits(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 	int ret = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 	int use_receive_queue = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 	struct smbd_response *response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 	struct smbd_connection *info =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 		container_of(work, struct smbd_connection,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			post_send_credits_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 	if (info->transport_status != SMBD_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 		wake_up(&info->wait_receive_queues);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 	if (info->receive_credit_target >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 		atomic_read(&info->receive_credits)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 		while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 			if (use_receive_queue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 				response = get_receive_buffer(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 				response = get_empty_queue_buffer(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 			if (!response) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 				/* now switch to emtpy packet queue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 				if (use_receive_queue) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 					use_receive_queue = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 					continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 				} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 			response->type = SMBD_TRANSFER_DATA;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 			response->first_segment = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 			rc = smbd_post_recv(info, response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 				log_rdma_recv(ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 					"post_recv failed rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 				put_receive_buffer(info, response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 			ret++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 	spin_lock(&info->lock_new_credits_offered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 	info->new_credits_offered += ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 	spin_unlock(&info->lock_new_credits_offered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 	/* Promptly send an immediate packet as defined in [MS-SMBD] 3.1.1.1 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 	info->send_immediate = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 	if (atomic_read(&info->receive_credits) <
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 		info->receive_credit_target - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 		if (info->keep_alive_requested == KEEP_ALIVE_PENDING ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 		    info->send_immediate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 			log_keep_alive(INFO, "send an empty message\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 			smbd_post_send_empty(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) /* Called from softirq, when recv is done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) static void recv_done(struct ib_cq *cq, struct ib_wc *wc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 	struct smbd_data_transfer *data_transfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 	struct smbd_response *response =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 		container_of(wc->wr_cqe, struct smbd_response, cqe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) 	struct smbd_connection *info = response->info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 	int data_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450) 	log_rdma_recv(INFO, "response=%p type=%d wc status=%d wc opcode %d byte_len=%d pkey_index=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451) 		      response, response->type, wc->status, wc->opcode,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 		      wc->byte_len, wc->pkey_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) 	if (wc->status != IB_WC_SUCCESS || wc->opcode != IB_WC_RECV) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 		log_rdma_recv(INFO, "wc->status=%d opcode=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) 			wc->status, wc->opcode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457) 		smbd_disconnect_rdma_connection(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458) 		goto error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) 	ib_dma_sync_single_for_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 		wc->qp->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 		response->sge.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 		response->sge.length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	switch (response->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) 	/* SMBD negotiation response */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 	case SMBD_NEGOTIATE_RESP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) 		dump_smbd_negotiate_resp(smbd_response_payload(response));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) 		info->full_packet_received = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 		info->negotiate_done =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 			process_negotiation_response(response, wc->byte_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 		complete(&info->negotiate_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 	/* SMBD data transfer packet */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	case SMBD_TRANSFER_DATA:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 		data_transfer = smbd_response_payload(response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 		data_length = le32_to_cpu(data_transfer->data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		 * If this is a packet with data playload place the data in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 		 * reassembly queue and wake up the reading thread
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		if (data_length) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 			if (info->full_packet_received)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 				response->first_segment = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 			if (le32_to_cpu(data_transfer->remaining_data_length))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 				info->full_packet_received = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 			else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 				info->full_packet_received = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 			enqueue_reassembly(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 				info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 				response,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 				data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			put_empty_packet(info, response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 		if (data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) 			wake_up_interruptible(&info->wait_reassembly_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) 		atomic_dec(&info->receive_credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506) 		info->receive_credit_target =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507) 			le16_to_cpu(data_transfer->credits_requested);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508) 		if (le16_to_cpu(data_transfer->credits_granted)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509) 			atomic_add(le16_to_cpu(data_transfer->credits_granted),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510) 				&info->send_credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512) 			 * We have new send credits granted from remote peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 			 * If any sender is waiting for credits, unblock it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) 			wake_up_interruptible(&info->wait_send_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) 		log_incoming(INFO, "data flags %d data_offset %d data_length %d remaining_data_length %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) 			     le16_to_cpu(data_transfer->flags),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) 			     le32_to_cpu(data_transfer->data_offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) 			     le32_to_cpu(data_transfer->data_length),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) 			     le32_to_cpu(data_transfer->remaining_data_length));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) 		/* Send a KEEP_ALIVE response right away if requested */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) 		info->keep_alive_requested = KEEP_ALIVE_NONE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) 		if (le16_to_cpu(data_transfer->flags) &
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) 				SMB_DIRECT_RESPONSE_REQUESTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 			info->keep_alive_requested = KEEP_ALIVE_PENDING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) 		log_rdma_recv(ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 			"unexpected response type=%d\n", response->type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) 	put_receive_buffer(info, response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) static struct rdma_cm_id *smbd_create_id(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) 		struct smbd_connection *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 		struct sockaddr *dstaddr, int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546) 	struct rdma_cm_id *id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548) 	__be16 *sport;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) 	id = rdma_create_id(&init_net, smbd_conn_upcall, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) 		RDMA_PS_TCP, IB_QPT_RC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	if (IS_ERR(id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 		rc = PTR_ERR(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 		log_rdma_event(ERR, "rdma_create_id() failed %i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 		return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 	if (dstaddr->sa_family == AF_INET6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		sport = &((struct sockaddr_in6 *)dstaddr)->sin6_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 		sport = &((struct sockaddr_in *)dstaddr)->sin_port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 	*sport = htons(port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 	init_completion(&info->ri_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 	info->ri_rc = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 	rc = rdma_resolve_addr(id, NULL, (struct sockaddr *)dstaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		RDMA_RESOLVE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		log_rdma_event(ERR, "rdma_resolve_addr() failed %i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) 	wait_for_completion_interruptible_timeout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 		&info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) 	rc = info->ri_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578) 		log_rdma_event(ERR, "rdma_resolve_addr() completed %i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 	info->ri_rc = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 	rc = rdma_resolve_route(id, RDMA_RESOLVE_TIMEOUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 		log_rdma_event(ERR, "rdma_resolve_route() failed %i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 	wait_for_completion_interruptible_timeout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 		&info->ri_done, msecs_to_jiffies(RDMA_RESOLVE_TIMEOUT));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	rc = info->ri_rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 		log_rdma_event(ERR, "rdma_resolve_route() completed %i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	return id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	rdma_destroy_id(id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	return ERR_PTR(rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604)  * Test if FRWR (Fast Registration Work Requests) is supported on the device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605)  * This implementation requries FRWR on RDMA read/write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606)  * return value: true if it is supported
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) static bool frwr_is_supported(struct ib_device_attr *attrs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 	if (!(attrs->device_cap_flags & IB_DEVICE_MEM_MGT_EXTENSIONS))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 	if (attrs->max_fast_reg_page_list_len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 		return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	return true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) static int smbd_ia_open(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 		struct smbd_connection *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 		struct sockaddr *dstaddr, int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 	info->id = smbd_create_id(info, dstaddr, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	if (IS_ERR(info->id)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 		rc = PTR_ERR(info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) 	if (!frwr_is_supported(&info->id->device->attrs)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 		log_rdma_event(ERR, "Fast Registration Work Requests (FRWR) is not supported\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) 		log_rdma_event(ERR, "Device capability flags = %llx max_fast_reg_page_list_len = %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 			       info->id->device->attrs.device_cap_flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 			       info->id->device->attrs.max_fast_reg_page_list_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 		rc = -EPROTONOSUPPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 		goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	info->max_frmr_depth = min_t(int,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 		smbd_max_frmr_depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		info->id->device->attrs.max_fast_reg_page_list_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 	info->mr_type = IB_MR_TYPE_MEM_REG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 	if (info->id->device->attrs.device_cap_flags & IB_DEVICE_SG_GAPS_REG)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 		info->mr_type = IB_MR_TYPE_SG_GAPS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	info->pd = ib_alloc_pd(info->id->device, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 	if (IS_ERR(info->pd)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		rc = PTR_ERR(info->pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 		log_rdma_event(ERR, "ib_alloc_pd() returned %d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 		goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 	rdma_destroy_id(info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 	info->id = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662)  * Send a negotiation request message to the peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663)  * The negotiation procedure is in [MS-SMBD] 3.1.5.2 and 3.1.5.3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664)  * After negotiation, the transport is connected and ready for
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665)  * carrying upper layer SMB payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) static int smbd_post_send_negotiate_req(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 	struct ib_send_wr send_wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 	int rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	struct smbd_request *request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 	struct smbd_negotiate_req *packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 	request = mempool_alloc(info->request_mempool, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 	if (!request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 	request->info = info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 	packet = smbd_request_payload(request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 	packet->min_version = cpu_to_le16(SMBD_V1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 	packet->max_version = cpu_to_le16(SMBD_V1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 	packet->reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 	packet->credits_requested = cpu_to_le16(info->send_credit_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 	packet->preferred_send_size = cpu_to_le32(info->max_send_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 	packet->max_receive_size = cpu_to_le32(info->max_receive_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 	packet->max_fragmented_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 		cpu_to_le32(info->max_fragmented_recv_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 	request->num_sge = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 	request->sge[0].addr = ib_dma_map_single(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 				info->id->device, (void *)packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 				sizeof(*packet), DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 	if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 		goto dma_mapping_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 	request->sge[0].length = sizeof(*packet);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 	request->sge[0].lkey = info->pd->local_dma_lkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 	ib_dma_sync_single_for_device(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		info->id->device, request->sge[0].addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 		request->sge[0].length, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 	request->cqe.done = send_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) 	send_wr.next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 	send_wr.wr_cqe = &request->cqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) 	send_wr.sg_list = request->sge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) 	send_wr.num_sge = request->num_sge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	send_wr.opcode = IB_WR_SEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	send_wr.send_flags = IB_SEND_SIGNALED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	log_rdma_send(INFO, "sge addr=%llx length=%x lkey=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 		request->sge[0].addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 		request->sge[0].length, request->sge[0].lkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	atomic_inc(&info->send_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 	rc = ib_post_send(info->id->qp, &send_wr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) 	/* if we reach here, post send failed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 	log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) 	atomic_dec(&info->send_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727) 	ib_dma_unmap_single(info->id->device, request->sge[0].addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728) 		request->sge[0].length, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) 	smbd_disconnect_rdma_connection(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) dma_mapping_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 	mempool_free(request, info->request_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738)  * Extend the credits to remote peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739)  * This implements [MS-SMBD] 3.1.5.9
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740)  * The idea is that we should extend credits to remote peer as quickly as
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741)  * it's allowed, to maintain data flow. We allocate as much receive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742)  * buffer as possible, and extend the receive credits to remote peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743)  * return value: the new credtis being granted.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) static int manage_credits_prior_sending(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	int new_credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	spin_lock(&info->lock_new_credits_offered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 	new_credits = info->new_credits_offered;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) 	info->new_credits_offered = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752) 	spin_unlock(&info->lock_new_credits_offered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 	return new_credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758)  * Check if we need to send a KEEP_ALIVE message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759)  * The idle connection timer triggers a KEEP_ALIVE message when expires
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760)  * SMB_DIRECT_RESPONSE_REQUESTED is set in the message flag to have peer send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761)  * back a response.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762)  * return value:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763)  * 1 if SMB_DIRECT_RESPONSE_REQUESTED needs to be set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764)  * 0: otherwise
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) static int manage_keep_alive_before_sending(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) 	if (info->keep_alive_requested == KEEP_ALIVE_PENDING) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) 		info->keep_alive_requested = KEEP_ALIVE_SENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 		return 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) /* Post the send request */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) static int smbd_post_send(struct smbd_connection *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 		struct smbd_request *request)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 	struct ib_send_wr send_wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 	int rc, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 	for (i = 0; i < request->num_sge; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 		log_rdma_send(INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 			"rdma_request sge[%d] addr=%llu length=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) 			i, request->sge[i].addr, request->sge[i].length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 		ib_dma_sync_single_for_device(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) 			info->id->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) 			request->sge[i].addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 			request->sge[i].length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 			DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 	request->cqe.done = send_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 	send_wr.next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	send_wr.wr_cqe = &request->cqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	send_wr.sg_list = request->sge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) 	send_wr.num_sge = request->num_sge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 	send_wr.opcode = IB_WR_SEND;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) 	send_wr.send_flags = IB_SEND_SIGNALED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	rc = ib_post_send(info->id->qp, &send_wr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 		log_rdma_send(ERR, "ib_post_send failed rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 		smbd_disconnect_rdma_connection(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 		rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		/* Reset timer for idle connection after packet is sent */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		mod_delayed_work(info->workqueue, &info->idle_timer_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 			info->keep_alive_interval*HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) static int smbd_post_send_sgl(struct smbd_connection *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	struct scatterlist *sgl, int data_length, int remaining_data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	int num_sgs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 	int i, rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	int header_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 	struct smbd_request *request;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 	struct smbd_data_transfer *packet;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	int new_credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	struct scatterlist *sg;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) wait_credit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	/* Wait for send credits. A SMBD packet needs one credit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	rc = wait_event_interruptible(info->wait_send_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 		atomic_read(&info->send_credits) > 0 ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 		info->transport_status != SMBD_CONNECTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 		goto err_wait_credit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 	if (info->transport_status != SMBD_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 		log_outgoing(ERR, "disconnected not sending on wait_credit\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 		goto err_wait_credit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 	if (unlikely(atomic_dec_return(&info->send_credits) < 0)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 		atomic_inc(&info->send_credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		goto wait_credit;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) wait_send_queue:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	wait_event(info->wait_post_send,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 		atomic_read(&info->send_pending) < info->send_credit_target ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 		info->transport_status != SMBD_CONNECTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 	if (info->transport_status != SMBD_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 		log_outgoing(ERR, "disconnected not sending on wait_send_queue\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 		goto err_wait_send_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 	if (unlikely(atomic_inc_return(&info->send_pending) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 				info->send_credit_target)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		atomic_dec(&info->send_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 		goto wait_send_queue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 	request = mempool_alloc(info->request_mempool, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 	if (!request) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		rc = -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 		goto err_alloc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 	request->info = info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 	/* Fill in the packet header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 	packet = smbd_request_payload(request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 	packet->credits_requested = cpu_to_le16(info->send_credit_target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 	new_credits = manage_credits_prior_sending(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 	atomic_add(new_credits, &info->receive_credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 	packet->credits_granted = cpu_to_le16(new_credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	info->send_immediate = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 	packet->flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 	if (manage_keep_alive_before_sending(info))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 		packet->flags |= cpu_to_le16(SMB_DIRECT_RESPONSE_REQUESTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 	packet->reserved = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 	if (!data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 		packet->data_offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		packet->data_offset = cpu_to_le32(24);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 	packet->data_length = cpu_to_le32(data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 	packet->remaining_data_length = cpu_to_le32(remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	packet->padding = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 	log_outgoing(INFO, "credits_requested=%d credits_granted=%d data_offset=%d data_length=%d remaining_data_length=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 		     le16_to_cpu(packet->credits_requested),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		     le16_to_cpu(packet->credits_granted),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 		     le32_to_cpu(packet->data_offset),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		     le32_to_cpu(packet->data_length),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 		     le32_to_cpu(packet->remaining_data_length));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 	/* Map the packet to DMA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	header_length = sizeof(struct smbd_data_transfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 	/* If this is a packet without payload, don't send padding */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 	if (!data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 		header_length = offsetof(struct smbd_data_transfer, padding);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 	request->num_sge = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 	request->sge[0].addr = ib_dma_map_single(info->id->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 						 (void *)packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 						 header_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 						 DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	if (ib_dma_mapping_error(info->id->device, request->sge[0].addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 		rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		request->sge[0].addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 		goto err_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) 	request->sge[0].length = header_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	request->sge[0].lkey = info->pd->local_dma_lkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	/* Fill in the packet data payload */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) 	num_sgs = sgl ? sg_nents(sgl) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 	for_each_sg(sgl, sg, num_sgs, i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) 		request->sge[i+1].addr =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) 			ib_dma_map_page(info->id->device, sg_page(sg),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 			       sg->offset, sg->length, DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 		if (ib_dma_mapping_error(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 				info->id->device, request->sge[i+1].addr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 			rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 			request->sge[i+1].addr = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 			goto err_dma;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 		request->sge[i+1].length = sg->length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		request->sge[i+1].lkey = info->pd->local_dma_lkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 		request->num_sge++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 	rc = smbd_post_send(info, request);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) err_dma:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 	for (i = 0; i < request->num_sge; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 		if (request->sge[i].addr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 			ib_dma_unmap_single(info->id->device,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 					    request->sge[i].addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 					    request->sge[i].length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 					    DMA_TO_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	mempool_free(request, info->request_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	/* roll back receive credits and credits to be offered */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 	spin_lock(&info->lock_new_credits_offered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	info->new_credits_offered += new_credits;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 	spin_unlock(&info->lock_new_credits_offered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 	atomic_sub(new_credits, &info->receive_credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) err_alloc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	if (atomic_dec_and_test(&info->send_pending))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 		wake_up(&info->wait_send_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) err_wait_send_queue:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	/* roll back send credits and pending */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 	atomic_inc(&info->send_credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) err_wait_credit:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968)  * Send a page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969)  * page: the page to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970)  * offset: offset in the page to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971)  * size: length in the page to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972)  * remaining_data_length: remaining data to send in this payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) static int smbd_post_send_page(struct smbd_connection *info, struct page *page,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 		unsigned long offset, size_t size, int remaining_data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	struct scatterlist sgl;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 	sg_init_table(&sgl, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	sg_set_page(&sgl, page, size, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	return smbd_post_send_sgl(info, &sgl, size, remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986)  * Send an empty message
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987)  * Empty message is used to extend credits to peer to for keep live
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988)  * while there is no upper layer payload to send at the time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) static int smbd_post_send_empty(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	info->count_send_empty++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) 	return smbd_post_send_sgl(info, NULL, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997)  * Send a data buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998)  * iov: the iov array describing the data buffers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999)  * n_vec: number of iov array
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000)  * remaining_data_length: remaining data to send following this packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001)  * in segmented SMBD packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) static int smbd_post_send_data(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 	struct smbd_connection *info, struct kvec *iov, int n_vec,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	int remaining_data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	u32 data_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	struct scatterlist sgl[SMBDIRECT_MAX_SGE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	if (n_vec > SMBDIRECT_MAX_SGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		cifs_dbg(VFS, "Can't fit data to SGL, n_vec=%d\n", n_vec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) 	sg_init_table(sgl, n_vec);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 	for (i = 0; i < n_vec; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) 		data_length += iov[i].iov_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 		sg_set_buf(&sgl[i], iov[i].iov_base, iov[i].iov_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	return smbd_post_send_sgl(info, sgl, data_length, remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026)  * Post a receive request to the transport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027)  * The remote peer can only send data when a receive request is posted
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028)  * The interaction is controlled by send/receive credit system
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) static int smbd_post_recv(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		struct smbd_connection *info, struct smbd_response *response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 	struct ib_recv_wr recv_wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 	int rc = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 	response->sge.addr = ib_dma_map_single(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 				info->id->device, response->packet,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 				info->max_receive_size, DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 	if (ib_dma_mapping_error(info->id->device, response->sge.addr))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	response->sge.length = info->max_receive_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 	response->sge.lkey = info->pd->local_dma_lkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	response->cqe.done = recv_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 	recv_wr.wr_cqe = &response->cqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) 	recv_wr.next = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) 	recv_wr.sg_list = &response->sge;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	recv_wr.num_sge = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 	rc = ib_post_recv(info->id->qp, &recv_wr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) 		ib_dma_unmap_single(info->id->device, response->sge.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1055) 				    response->sge.length, DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1056) 		smbd_disconnect_rdma_connection(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1057) 		log_rdma_recv(ERR, "ib_post_recv failed rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1058) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1059) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1060) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1061) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1062) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1063) /* Perform SMBD negotiate according to [MS-SMBD] 3.1.5.2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1064) static int smbd_negotiate(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1065) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1066) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1067) 	struct smbd_response *response = get_receive_buffer(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1068) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1069) 	response->type = SMBD_NEGOTIATE_RESP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1070) 	rc = smbd_post_recv(info, response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1071) 	log_rdma_event(INFO, "smbd_post_recv rc=%d iov.addr=%llx iov.length=%x iov.lkey=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1072) 		       rc, response->sge.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1073) 		       response->sge.length, response->sge.lkey);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1074) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1075) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1076) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1077) 	init_completion(&info->negotiate_completion);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1078) 	info->negotiate_done = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1079) 	rc = smbd_post_send_negotiate_req(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1080) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1081) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1082) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1083) 	rc = wait_for_completion_interruptible_timeout(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1084) 		&info->negotiate_completion, SMBD_NEGOTIATE_TIMEOUT * HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1085) 	log_rdma_event(INFO, "wait_for_completion_timeout rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1086) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1087) 	if (info->negotiate_done)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1088) 		return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1089) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1090) 	if (rc == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1091) 		rc = -ETIMEDOUT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1092) 	else if (rc == -ERESTARTSYS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1093) 		rc = -EINTR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1094) 	else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1095) 		rc = -ENOTCONN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1096) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1097) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1098) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1099) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1100) static void put_empty_packet(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1101) 		struct smbd_connection *info, struct smbd_response *response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1102) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1103) 	spin_lock(&info->empty_packet_queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1104) 	list_add_tail(&response->list, &info->empty_packet_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1105) 	info->count_empty_packet_queue++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1106) 	spin_unlock(&info->empty_packet_queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1107) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1108) 	queue_work(info->workqueue, &info->post_send_credits_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1109) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1110) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1111) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1112)  * Implement Connection.FragmentReassemblyBuffer defined in [MS-SMBD] 3.1.1.1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1113)  * This is a queue for reassembling upper layer payload and present to upper
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1114)  * layer. All the inncoming payload go to the reassembly queue, regardless of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1115)  * if reassembly is required. The uuper layer code reads from the queue for all
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1116)  * incoming payloads.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1117)  * Put a received packet to the reassembly queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1118)  * response: the packet received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1119)  * data_length: the size of payload in this packet
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1120)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1121) static void enqueue_reassembly(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1122) 	struct smbd_connection *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1123) 	struct smbd_response *response,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1124) 	int data_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1125) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1126) 	spin_lock(&info->reassembly_queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1127) 	list_add_tail(&response->list, &info->reassembly_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1128) 	info->reassembly_queue_length++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1129) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1130) 	 * Make sure reassembly_data_length is updated after list and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1131) 	 * reassembly_queue_length are updated. On the dequeue side
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1132) 	 * reassembly_data_length is checked without a lock to determine
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1133) 	 * if reassembly_queue_length and list is up to date
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1134) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1135) 	virt_wmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1136) 	info->reassembly_data_length += data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1137) 	spin_unlock(&info->reassembly_queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1138) 	info->count_reassembly_queue++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1139) 	info->count_enqueue_reassembly_queue++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1141) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1142) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1143)  * Get the first entry at the front of reassembly queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1144)  * Caller is responsible for locking
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1145)  * return value: the first entry if any, NULL if queue is empty
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1146)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1147) static struct smbd_response *_get_first_reassembly(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1149) 	struct smbd_response *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1151) 	if (!list_empty(&info->reassembly_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1152) 		ret = list_first_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1153) 			&info->reassembly_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1154) 			struct smbd_response, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1155) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1156) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1159) static struct smbd_response *get_empty_queue_buffer(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1160) 		struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1162) 	struct smbd_response *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1163) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1165) 	spin_lock_irqsave(&info->empty_packet_queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1166) 	if (!list_empty(&info->empty_packet_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1167) 		ret = list_first_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1168) 			&info->empty_packet_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1169) 			struct smbd_response, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1170) 		list_del(&ret->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1171) 		info->count_empty_packet_queue--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1172) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1173) 	spin_unlock_irqrestore(&info->empty_packet_queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1175) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1176) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1177) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1178) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1179)  * Get a receive buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1180)  * For each remote send, we need to post a receive. The receive buffers are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1181)  * pre-allocated in advance.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1182)  * return value: the receive buffer, NULL if none is available
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1183)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1184) static struct smbd_response *get_receive_buffer(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1185) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1186) 	struct smbd_response *ret = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1187) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1189) 	spin_lock_irqsave(&info->receive_queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1190) 	if (!list_empty(&info->receive_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1191) 		ret = list_first_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1192) 			&info->receive_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1193) 			struct smbd_response, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1194) 		list_del(&ret->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1195) 		info->count_receive_queue--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1196) 		info->count_get_receive_buffer++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1197) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1198) 	spin_unlock_irqrestore(&info->receive_queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1199) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1200) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1201) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1203) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1204)  * Return a receive buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1205)  * Upon returning of a receive buffer, we can post new receive and extend
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1206)  * more receive credits to remote peer. This is done immediately after a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1207)  * receive buffer is returned.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1208)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1209) static void put_receive_buffer(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1210) 	struct smbd_connection *info, struct smbd_response *response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1211) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1212) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1214) 	ib_dma_unmap_single(info->id->device, response->sge.addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1215) 		response->sge.length, DMA_FROM_DEVICE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1216) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1217) 	spin_lock_irqsave(&info->receive_queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1218) 	list_add_tail(&response->list, &info->receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1219) 	info->count_receive_queue++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1220) 	info->count_put_receive_buffer++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1221) 	spin_unlock_irqrestore(&info->receive_queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1223) 	queue_work(info->workqueue, &info->post_send_credits_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1224) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1226) /* Preallocate all receive buffer on transport establishment */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1227) static int allocate_receive_buffers(struct smbd_connection *info, int num_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1228) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1229) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1230) 	struct smbd_response *response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1231) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1232) 	INIT_LIST_HEAD(&info->reassembly_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1233) 	spin_lock_init(&info->reassembly_queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1234) 	info->reassembly_data_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1235) 	info->reassembly_queue_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1236) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1237) 	INIT_LIST_HEAD(&info->receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1238) 	spin_lock_init(&info->receive_queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1239) 	info->count_receive_queue = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1241) 	INIT_LIST_HEAD(&info->empty_packet_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1242) 	spin_lock_init(&info->empty_packet_queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1243) 	info->count_empty_packet_queue = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1245) 	init_waitqueue_head(&info->wait_receive_queues);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1246) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1247) 	for (i = 0; i < num_buf; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1248) 		response = mempool_alloc(info->response_mempool, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1249) 		if (!response)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1250) 			goto allocate_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1251) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1252) 		response->info = info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1253) 		list_add_tail(&response->list, &info->receive_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1254) 		info->count_receive_queue++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1255) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1256) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1257) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1259) allocate_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1260) 	while (!list_empty(&info->receive_queue)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1261) 		response = list_first_entry(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1262) 				&info->receive_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1263) 				struct smbd_response, list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1264) 		list_del(&response->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1265) 		info->count_receive_queue--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1267) 		mempool_free(response, info->response_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1268) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1269) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1270) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1271) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1272) static void destroy_receive_buffers(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1273) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1274) 	struct smbd_response *response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1275) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1276) 	while ((response = get_receive_buffer(info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1277) 		mempool_free(response, info->response_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1278) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1279) 	while ((response = get_empty_queue_buffer(info)))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1280) 		mempool_free(response, info->response_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1282) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1283) /* Implement idle connection timer [MS-SMBD] 3.1.6.2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1284) static void idle_connection_timer(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1286) 	struct smbd_connection *info = container_of(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1287) 					work, struct smbd_connection,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1288) 					idle_timer_work.work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1289) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1290) 	if (info->keep_alive_requested != KEEP_ALIVE_NONE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1291) 		log_keep_alive(ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1292) 			"error status info->keep_alive_requested=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1293) 			info->keep_alive_requested);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1294) 		smbd_disconnect_rdma_connection(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1295) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1296) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1297) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1298) 	log_keep_alive(INFO, "about to send an empty idle message\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1299) 	smbd_post_send_empty(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1301) 	/* Setup the next idle timeout work */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1302) 	queue_delayed_work(info->workqueue, &info->idle_timer_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1303) 			info->keep_alive_interval*HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1304) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1306) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1307)  * Destroy the transport and related RDMA and memory resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1308)  * Need to go through all the pending counters and make sure on one is using
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1309)  * the transport while it is destroyed
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1310)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1311) void smbd_destroy(struct TCP_Server_Info *server)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1312) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1313) 	struct smbd_connection *info = server->smbd_conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1314) 	struct smbd_response *response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1315) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1316) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1317) 	if (!info) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1318) 		log_rdma_event(INFO, "rdma session already destroyed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1319) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1322) 	log_rdma_event(INFO, "destroying rdma session\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1323) 	if (info->transport_status != SMBD_DISCONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1324) 		rdma_disconnect(server->smbd_conn->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1325) 		log_rdma_event(INFO, "wait for transport being disconnected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1326) 		wait_event_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1327) 			info->disconn_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1328) 			info->transport_status == SMBD_DISCONNECTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1329) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1330) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1331) 	log_rdma_event(INFO, "destroying qp\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1332) 	ib_drain_qp(info->id->qp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1333) 	rdma_destroy_qp(info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1335) 	log_rdma_event(INFO, "cancelling idle timer\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1336) 	cancel_delayed_work_sync(&info->idle_timer_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1337) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1338) 	log_rdma_event(INFO, "wait for all send posted to IB to finish\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1339) 	wait_event(info->wait_send_pending,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1340) 		atomic_read(&info->send_pending) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1342) 	/* It's not posssible for upper layer to get to reassembly */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1343) 	log_rdma_event(INFO, "drain the reassembly queue\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1344) 	do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1345) 		spin_lock_irqsave(&info->reassembly_queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1346) 		response = _get_first_reassembly(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1347) 		if (response) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1348) 			list_del(&response->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1349) 			spin_unlock_irqrestore(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1350) 				&info->reassembly_queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1351) 			put_receive_buffer(info, response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1352) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1353) 			spin_unlock_irqrestore(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1354) 				&info->reassembly_queue_lock, flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1355) 	} while (response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1356) 	info->reassembly_data_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1358) 	log_rdma_event(INFO, "free receive buffers\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1359) 	wait_event(info->wait_receive_queues,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1360) 		info->count_receive_queue + info->count_empty_packet_queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1361) 			== info->receive_credit_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1362) 	destroy_receive_buffers(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1364) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1365) 	 * For performance reasons, memory registration and deregistration
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1366) 	 * are not locked by srv_mutex. It is possible some processes are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1367) 	 * blocked on transport srv_mutex while holding memory registration.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1368) 	 * Release the transport srv_mutex to allow them to hit the failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1369) 	 * path when sending data, and then release memory registartions.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1370) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1371) 	log_rdma_event(INFO, "freeing mr list\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1372) 	wake_up_interruptible_all(&info->wait_mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1373) 	while (atomic_read(&info->mr_used_count)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1374) 		mutex_unlock(&server->srv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1375) 		msleep(1000);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1376) 		mutex_lock(&server->srv_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1377) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1378) 	destroy_mr_list(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1380) 	ib_free_cq(info->send_cq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1381) 	ib_free_cq(info->recv_cq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1382) 	ib_dealloc_pd(info->pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1383) 	rdma_destroy_id(info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1384) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1385) 	/* free mempools */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1386) 	mempool_destroy(info->request_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1387) 	kmem_cache_destroy(info->request_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1388) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1389) 	mempool_destroy(info->response_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1390) 	kmem_cache_destroy(info->response_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1392) 	info->transport_status = SMBD_DESTROYED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1393) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1394) 	destroy_workqueue(info->workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1395) 	log_rdma_event(INFO,  "rdma session destroyed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1396) 	kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1399) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1400)  * Reconnect this SMBD connection, called from upper layer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1401)  * return value: 0 on success, or actual error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1402)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1403) int smbd_reconnect(struct TCP_Server_Info *server)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1404) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1405) 	log_rdma_event(INFO, "reconnecting rdma session\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1406) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1407) 	if (!server->smbd_conn) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1408) 		log_rdma_event(INFO, "rdma session already destroyed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1409) 		goto create_conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1410) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1411) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1412) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1413) 	 * This is possible if transport is disconnected and we haven't received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1414) 	 * notification from RDMA, but upper layer has detected timeout
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1415) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1416) 	if (server->smbd_conn->transport_status == SMBD_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1417) 		log_rdma_event(INFO, "disconnecting transport\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1418) 		smbd_destroy(server);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1419) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1420) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1421) create_conn:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1422) 	log_rdma_event(INFO, "creating rdma session\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1423) 	server->smbd_conn = smbd_get_connection(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1424) 		server, (struct sockaddr *) &server->dstaddr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1425) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1426) 	if (server->smbd_conn)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1427) 		cifs_dbg(VFS, "RDMA transport re-established\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1428) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1429) 	return server->smbd_conn ? 0 : -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1430) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1431) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1432) static void destroy_caches_and_workqueue(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1433) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1434) 	destroy_receive_buffers(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1435) 	destroy_workqueue(info->workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1436) 	mempool_destroy(info->response_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1437) 	kmem_cache_destroy(info->response_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1438) 	mempool_destroy(info->request_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1439) 	kmem_cache_destroy(info->request_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1440) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1441) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1442) #define MAX_NAME_LEN	80
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1443) static int allocate_caches_and_workqueue(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1444) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1445) 	char name[MAX_NAME_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1446) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1447) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1448) 	scnprintf(name, MAX_NAME_LEN, "smbd_request_%p", info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1449) 	info->request_cache =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1450) 		kmem_cache_create(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1451) 			name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1452) 			sizeof(struct smbd_request) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1453) 				sizeof(struct smbd_data_transfer),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1454) 			0, SLAB_HWCACHE_ALIGN, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1455) 	if (!info->request_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1456) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1457) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1458) 	info->request_mempool =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1459) 		mempool_create(info->send_credit_target, mempool_alloc_slab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1460) 			mempool_free_slab, info->request_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1461) 	if (!info->request_mempool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1462) 		goto out1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1464) 	scnprintf(name, MAX_NAME_LEN, "smbd_response_%p", info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1465) 	info->response_cache =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1466) 		kmem_cache_create(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1467) 			name,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1468) 			sizeof(struct smbd_response) +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1469) 				info->max_receive_size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1470) 			0, SLAB_HWCACHE_ALIGN, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1471) 	if (!info->response_cache)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1472) 		goto out2;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1473) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1474) 	info->response_mempool =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1475) 		mempool_create(info->receive_credit_max, mempool_alloc_slab,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1476) 		       mempool_free_slab, info->response_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1477) 	if (!info->response_mempool)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1478) 		goto out3;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1480) 	scnprintf(name, MAX_NAME_LEN, "smbd_%p", info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1481) 	info->workqueue = create_workqueue(name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1482) 	if (!info->workqueue)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1483) 		goto out4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1485) 	rc = allocate_receive_buffers(info, info->receive_credit_max);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1486) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1487) 		log_rdma_event(ERR, "failed to allocate receive buffers\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1488) 		goto out5;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1489) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1490) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1491) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1493) out5:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1494) 	destroy_workqueue(info->workqueue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1495) out4:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1496) 	mempool_destroy(info->response_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1497) out3:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1498) 	kmem_cache_destroy(info->response_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1499) out2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1500) 	mempool_destroy(info->request_mempool);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1501) out1:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1502) 	kmem_cache_destroy(info->request_cache);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1503) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1504) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1505) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1506) /* Create a SMBD connection, called by upper layer */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1507) static struct smbd_connection *_smbd_get_connection(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1508) 	struct TCP_Server_Info *server, struct sockaddr *dstaddr, int port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1509) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1510) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1511) 	struct smbd_connection *info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1512) 	struct rdma_conn_param conn_param;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1513) 	struct ib_qp_init_attr qp_attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1514) 	struct sockaddr_in *addr_in = (struct sockaddr_in *) dstaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1515) 	struct ib_port_immutable port_immutable;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1516) 	u32 ird_ord_hdr[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1518) 	info = kzalloc(sizeof(struct smbd_connection), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1519) 	if (!info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1520) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1521) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1522) 	info->transport_status = SMBD_CONNECTING;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1523) 	rc = smbd_ia_open(info, dstaddr, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1524) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1525) 		log_rdma_event(INFO, "smbd_ia_open rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1526) 		goto create_id_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1527) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1529) 	if (smbd_send_credit_target > info->id->device->attrs.max_cqe ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1530) 	    smbd_send_credit_target > info->id->device->attrs.max_qp_wr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1531) 		log_rdma_event(ERR, "consider lowering send_credit_target = %d. Possible CQE overrun, device reporting max_cpe %d max_qp_wr %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1532) 			       smbd_send_credit_target,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1533) 			       info->id->device->attrs.max_cqe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1534) 			       info->id->device->attrs.max_qp_wr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1535) 		goto config_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1536) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1537) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1538) 	if (smbd_receive_credit_max > info->id->device->attrs.max_cqe ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1539) 	    smbd_receive_credit_max > info->id->device->attrs.max_qp_wr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1540) 		log_rdma_event(ERR, "consider lowering receive_credit_max = %d. Possible CQE overrun, device reporting max_cpe %d max_qp_wr %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1541) 			       smbd_receive_credit_max,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1542) 			       info->id->device->attrs.max_cqe,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1543) 			       info->id->device->attrs.max_qp_wr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1544) 		goto config_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1545) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1546) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1547) 	info->receive_credit_max = smbd_receive_credit_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1548) 	info->send_credit_target = smbd_send_credit_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1549) 	info->max_send_size = smbd_max_send_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1550) 	info->max_fragmented_recv_size = smbd_max_fragmented_recv_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1551) 	info->max_receive_size = smbd_max_receive_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1552) 	info->keep_alive_interval = smbd_keep_alive_interval;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1553) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1554) 	if (info->id->device->attrs.max_send_sge < SMBDIRECT_MAX_SGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1555) 		log_rdma_event(ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1556) 			"warning: device max_send_sge = %d too small\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1557) 			info->id->device->attrs.max_send_sge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1558) 		log_rdma_event(ERR, "Queue Pair creation may fail\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1559) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1560) 	if (info->id->device->attrs.max_recv_sge < SMBDIRECT_MAX_SGE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1561) 		log_rdma_event(ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1562) 			"warning: device max_recv_sge = %d too small\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1563) 			info->id->device->attrs.max_recv_sge);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1564) 		log_rdma_event(ERR, "Queue Pair creation may fail\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1565) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1567) 	info->send_cq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1568) 	info->recv_cq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1569) 	info->send_cq =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1570) 		ib_alloc_cq_any(info->id->device, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1571) 				info->send_credit_target, IB_POLL_SOFTIRQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1572) 	if (IS_ERR(info->send_cq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1573) 		info->send_cq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1574) 		goto alloc_cq_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1575) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1576) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1577) 	info->recv_cq =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1578) 		ib_alloc_cq_any(info->id->device, info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1579) 				info->receive_credit_max, IB_POLL_SOFTIRQ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1580) 	if (IS_ERR(info->recv_cq)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1581) 		info->recv_cq = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1582) 		goto alloc_cq_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1583) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1584) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1585) 	memset(&qp_attr, 0, sizeof(qp_attr));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1586) 	qp_attr.event_handler = smbd_qp_async_error_upcall;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1587) 	qp_attr.qp_context = info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1588) 	qp_attr.cap.max_send_wr = info->send_credit_target;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1589) 	qp_attr.cap.max_recv_wr = info->receive_credit_max;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1590) 	qp_attr.cap.max_send_sge = SMBDIRECT_MAX_SGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1591) 	qp_attr.cap.max_recv_sge = SMBDIRECT_MAX_SGE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1592) 	qp_attr.cap.max_inline_data = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1593) 	qp_attr.sq_sig_type = IB_SIGNAL_REQ_WR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1594) 	qp_attr.qp_type = IB_QPT_RC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1595) 	qp_attr.send_cq = info->send_cq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1596) 	qp_attr.recv_cq = info->recv_cq;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1597) 	qp_attr.port_num = ~0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1598) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1599) 	rc = rdma_create_qp(info->id, info->pd, &qp_attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1600) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1601) 		log_rdma_event(ERR, "rdma_create_qp failed %i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1602) 		goto create_qp_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1603) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1604) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1605) 	memset(&conn_param, 0, sizeof(conn_param));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1606) 	conn_param.initiator_depth = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1607) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1608) 	conn_param.responder_resources =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1609) 		info->id->device->attrs.max_qp_rd_atom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1610) 			< SMBD_CM_RESPONDER_RESOURCES ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1611) 		info->id->device->attrs.max_qp_rd_atom :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1612) 		SMBD_CM_RESPONDER_RESOURCES;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1613) 	info->responder_resources = conn_param.responder_resources;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1614) 	log_rdma_mr(INFO, "responder_resources=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1615) 		info->responder_resources);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1616) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1617) 	/* Need to send IRD/ORD in private data for iWARP */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1618) 	info->id->device->ops.get_port_immutable(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1619) 		info->id->device, info->id->port_num, &port_immutable);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1620) 	if (port_immutable.core_cap_flags & RDMA_CORE_PORT_IWARP) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1621) 		ird_ord_hdr[0] = info->responder_resources;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1622) 		ird_ord_hdr[1] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1623) 		conn_param.private_data = ird_ord_hdr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1624) 		conn_param.private_data_len = sizeof(ird_ord_hdr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1625) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1626) 		conn_param.private_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1627) 		conn_param.private_data_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1628) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1629) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1630) 	conn_param.retry_count = SMBD_CM_RETRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1631) 	conn_param.rnr_retry_count = SMBD_CM_RNR_RETRY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1632) 	conn_param.flow_control = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1633) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1634) 	log_rdma_event(INFO, "connecting to IP %pI4 port %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1635) 		&addr_in->sin_addr, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1637) 	init_waitqueue_head(&info->conn_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1638) 	init_waitqueue_head(&info->disconn_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1639) 	init_waitqueue_head(&info->wait_reassembly_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1640) 	rc = rdma_connect(info->id, &conn_param);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1641) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1642) 		log_rdma_event(ERR, "rdma_connect() failed with %i\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1643) 		goto rdma_connect_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1644) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1646) 	wait_event_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1647) 		info->conn_wait, info->transport_status != SMBD_CONNECTING);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1648) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1649) 	if (info->transport_status != SMBD_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1650) 		log_rdma_event(ERR, "rdma_connect failed port=%d\n", port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1651) 		goto rdma_connect_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1652) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1653) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1654) 	log_rdma_event(INFO, "rdma_connect connected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1655) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1656) 	rc = allocate_caches_and_workqueue(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1657) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1658) 		log_rdma_event(ERR, "cache allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1659) 		goto allocate_cache_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1660) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1661) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1662) 	init_waitqueue_head(&info->wait_send_queue);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1663) 	INIT_DELAYED_WORK(&info->idle_timer_work, idle_connection_timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1664) 	queue_delayed_work(info->workqueue, &info->idle_timer_work,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1665) 		info->keep_alive_interval*HZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1667) 	init_waitqueue_head(&info->wait_send_pending);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1668) 	atomic_set(&info->send_pending, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1669) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1670) 	init_waitqueue_head(&info->wait_post_send);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1671) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1672) 	INIT_WORK(&info->disconnect_work, smbd_disconnect_rdma_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1673) 	INIT_WORK(&info->post_send_credits_work, smbd_post_send_credits);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1674) 	info->new_credits_offered = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1675) 	spin_lock_init(&info->lock_new_credits_offered);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1676) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1677) 	rc = smbd_negotiate(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1678) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1679) 		log_rdma_event(ERR, "smbd_negotiate rc=%d\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1680) 		goto negotiation_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1681) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1683) 	rc = allocate_mr_list(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1684) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1685) 		log_rdma_mr(ERR, "memory registration allocation failed\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1686) 		goto allocate_mr_failed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1687) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1688) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1689) 	return info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1690) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1691) allocate_mr_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1692) 	/* At this point, need to a full transport shutdown */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1693) 	smbd_destroy(server);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1694) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1695) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1696) negotiation_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1697) 	cancel_delayed_work_sync(&info->idle_timer_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1698) 	destroy_caches_and_workqueue(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1699) 	info->transport_status = SMBD_NEGOTIATE_FAILED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1700) 	init_waitqueue_head(&info->conn_wait);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1701) 	rdma_disconnect(info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1702) 	wait_event(info->conn_wait,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1703) 		info->transport_status == SMBD_DISCONNECTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1705) allocate_cache_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1706) rdma_connect_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1707) 	rdma_destroy_qp(info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1708) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1709) create_qp_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1710) alloc_cq_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1711) 	if (info->send_cq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1712) 		ib_free_cq(info->send_cq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1713) 	if (info->recv_cq)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1714) 		ib_free_cq(info->recv_cq);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1715) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1716) config_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1717) 	ib_dealloc_pd(info->pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1718) 	rdma_destroy_id(info->id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1719) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1720) create_id_failed:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1721) 	kfree(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1722) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1723) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1724) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1725) struct smbd_connection *smbd_get_connection(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1726) 	struct TCP_Server_Info *server, struct sockaddr *dstaddr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1727) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1728) 	struct smbd_connection *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1729) 	int port = SMBD_PORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1730) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1731) try_again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1732) 	ret = _smbd_get_connection(server, dstaddr, port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1734) 	/* Try SMB_PORT if SMBD_PORT doesn't work */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1735) 	if (!ret && port == SMBD_PORT) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1736) 		port = SMB_PORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1737) 		goto try_again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1738) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1739) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1740) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1741) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1742) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1743)  * Receive data from receive reassembly queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1744)  * All the incoming data packets are placed in reassembly queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1745)  * buf: the buffer to read data into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1746)  * size: the length of data to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1747)  * return value: actual data read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1748)  * Note: this implementation copies the data from reassebmly queue to receive
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1749)  * buffers used by upper layer. This is not the optimal code path. A better way
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1750)  * to do it is to not have upper layer allocate its receive buffers but rather
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1751)  * borrow the buffer from reassembly queue, and return it after data is
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1752)  * consumed. But this will require more changes to upper layer code, and also
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1753)  * need to consider packet boundaries while they still being reassembled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1754)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1755) static int smbd_recv_buf(struct smbd_connection *info, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1756) 		unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1757) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1758) 	struct smbd_response *response;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1759) 	struct smbd_data_transfer *data_transfer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1760) 	int to_copy, to_read, data_read, offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1761) 	u32 data_length, remaining_data_length, data_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1762) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1763) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1764) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1765) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1766) 	 * No need to hold the reassembly queue lock all the time as we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1767) 	 * the only one reading from the front of the queue. The transport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1768) 	 * may add more entries to the back of the queue at the same time
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1769) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1770) 	log_read(INFO, "size=%d info->reassembly_data_length=%d\n", size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1771) 		info->reassembly_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1772) 	if (info->reassembly_data_length >= size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1773) 		int queue_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1774) 		int queue_removed = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1775) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1776) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1777) 		 * Need to make sure reassembly_data_length is read before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1778) 		 * reading reassembly_queue_length and calling
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1779) 		 * _get_first_reassembly. This call is lock free
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1780) 		 * as we never read at the end of the queue which are being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1781) 		 * updated in SOFTIRQ as more data is received
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1782) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1783) 		virt_rmb();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1784) 		queue_length = info->reassembly_queue_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1785) 		data_read = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1786) 		to_read = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1787) 		offset = info->first_entry_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1788) 		while (data_read < size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1789) 			response = _get_first_reassembly(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1790) 			data_transfer = smbd_response_payload(response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1791) 			data_length = le32_to_cpu(data_transfer->data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1792) 			remaining_data_length =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1793) 				le32_to_cpu(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1794) 					data_transfer->remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1795) 			data_offset = le32_to_cpu(data_transfer->data_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1796) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1797) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1798) 			 * The upper layer expects RFC1002 length at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1799) 			 * beginning of the payload. Return it to indicate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1800) 			 * the total length of the packet. This minimize the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1801) 			 * change to upper layer packet processing logic. This
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1802) 			 * will be eventually remove when an intermediate
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1803) 			 * transport layer is added
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1804) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1805) 			if (response->first_segment && size == 4) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1806) 				unsigned int rfc1002_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1807) 					data_length + remaining_data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1808) 				*((__be32 *)buf) = cpu_to_be32(rfc1002_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1809) 				data_read = 4;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1810) 				response->first_segment = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1811) 				log_read(INFO, "returning rfc1002 length %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1812) 					rfc1002_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1813) 				goto read_rfc1002_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1814) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1815) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1816) 			to_copy = min_t(int, data_length - offset, to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1817) 			memcpy(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1818) 				buf + data_read,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1819) 				(char *)data_transfer + data_offset + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1820) 				to_copy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1821) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1822) 			/* move on to the next buffer? */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1823) 			if (to_copy == data_length - offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1824) 				queue_length--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1825) 				/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1826) 				 * No need to lock if we are not at the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1827) 				 * end of the queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1828) 				 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1829) 				if (queue_length)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1830) 					list_del(&response->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1831) 				else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1832) 					spin_lock_irq(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1833) 						&info->reassembly_queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1834) 					list_del(&response->list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1835) 					spin_unlock_irq(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1836) 						&info->reassembly_queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1837) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1838) 				queue_removed++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1839) 				info->count_reassembly_queue--;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1840) 				info->count_dequeue_reassembly_queue++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1841) 				put_receive_buffer(info, response);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1842) 				offset = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1843) 				log_read(INFO, "put_receive_buffer offset=0\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1844) 			} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1845) 				offset += to_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1847) 			to_read -= to_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1848) 			data_read += to_copy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1849) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1850) 			log_read(INFO, "_get_first_reassembly memcpy %d bytes data_transfer_length-offset=%d after that to_read=%d data_read=%d offset=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1851) 				 to_copy, data_length - offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1852) 				 to_read, data_read, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1853) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1854) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1855) 		spin_lock_irq(&info->reassembly_queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1856) 		info->reassembly_data_length -= data_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1857) 		info->reassembly_queue_length -= queue_removed;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1858) 		spin_unlock_irq(&info->reassembly_queue_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1859) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1860) 		info->first_entry_offset = offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1861) 		log_read(INFO, "returning to thread data_read=%d reassembly_data_length=%d first_entry_offset=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1862) 			 data_read, info->reassembly_data_length,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1863) 			 info->first_entry_offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1864) read_rfc1002_done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1865) 		return data_read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1866) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1867) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1868) 	log_read(INFO, "wait_event on more data\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1869) 	rc = wait_event_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1870) 		info->wait_reassembly_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1871) 		info->reassembly_data_length >= size ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1872) 			info->transport_status != SMBD_CONNECTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1873) 	/* Don't return any data if interrupted */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1874) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1875) 		return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1877) 	if (info->transport_status != SMBD_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1878) 		log_read(ERR, "disconnected\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1879) 		return -ECONNABORTED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1880) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1882) 	goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1883) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1884) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1885) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1886)  * Receive a page from receive reassembly queue
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1887)  * page: the page to read data into
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1888)  * to_read: the length of data to read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1889)  * return value: actual data read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1890)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1891) static int smbd_recv_page(struct smbd_connection *info,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1892) 		struct page *page, unsigned int page_offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1893) 		unsigned int to_read)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1894) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1895) 	int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1896) 	char *to_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1897) 	void *page_address;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1898) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1899) 	/* make sure we have the page ready for read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1900) 	ret = wait_event_interruptible(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1901) 		info->wait_reassembly_queue,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1902) 		info->reassembly_data_length >= to_read ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1903) 			info->transport_status != SMBD_CONNECTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1904) 	if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1905) 		return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1907) 	/* now we can read from reassembly queue and not sleep */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1908) 	page_address = kmap_atomic(page);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1909) 	to_address = (char *) page_address + page_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1910) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1911) 	log_read(INFO, "reading from page=%p address=%p to_read=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1912) 		page, to_address, to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1914) 	ret = smbd_recv_buf(info, to_address, to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1915) 	kunmap_atomic(page_address);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1916) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1917) 	return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1918) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1919) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1920) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1921)  * Receive data from transport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1922)  * msg: a msghdr point to the buffer, can be ITER_KVEC or ITER_BVEC
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1923)  * return: total bytes read, or 0. SMB Direct will not do partial read.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1924)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1925) int smbd_recv(struct smbd_connection *info, struct msghdr *msg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1926) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1927) 	char *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1928) 	struct page *page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1929) 	unsigned int to_read, page_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1930) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1931) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1932) 	if (iov_iter_rw(&msg->msg_iter) == WRITE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1933) 		/* It's a bug in upper layer to get there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1934) 		cifs_dbg(VFS, "Invalid msg iter dir %u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1935) 			 iov_iter_rw(&msg->msg_iter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1936) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1937) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1938) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1939) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1940) 	switch (iov_iter_type(&msg->msg_iter)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1941) 	case ITER_KVEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1942) 		buf = msg->msg_iter.kvec->iov_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1943) 		to_read = msg->msg_iter.kvec->iov_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1944) 		rc = smbd_recv_buf(info, buf, to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1945) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1946) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1947) 	case ITER_BVEC:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1948) 		page = msg->msg_iter.bvec->bv_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1949) 		page_offset = msg->msg_iter.bvec->bv_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1950) 		to_read = msg->msg_iter.bvec->bv_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1951) 		rc = smbd_recv_page(info, page, page_offset, to_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1952) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1953) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1954) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1955) 		/* It's a bug in upper layer to get there */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1956) 		cifs_dbg(VFS, "Invalid msg type %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1957) 			 iov_iter_type(&msg->msg_iter));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1958) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1959) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1960) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1961) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1962) 	/* SMBDirect will read it all or nothing */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1963) 	if (rc > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1964) 		msg->msg_iter.count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1965) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1966) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1967) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1968) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1969)  * Send data to transport
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1970)  * Each rqst is transported as a SMBDirect payload
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1971)  * rqst: the data to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1972)  * return value: 0 if successfully write, otherwise error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1973)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1974) int smbd_send(struct TCP_Server_Info *server,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1975) 	int num_rqst, struct smb_rqst *rqst_array)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1976) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1977) 	struct smbd_connection *info = server->smbd_conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1978) 	struct kvec vec;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1979) 	int nvecs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1980) 	int size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1981) 	unsigned int buflen, remaining_data_length;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1982) 	int start, i, j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1983) 	int max_iov_size =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1984) 		info->max_send_size - sizeof(struct smbd_data_transfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1985) 	struct kvec *iov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1986) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1987) 	struct smb_rqst *rqst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1988) 	int rqst_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1989) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1990) 	if (info->transport_status != SMBD_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1991) 		rc = -EAGAIN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1992) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1993) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1995) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1996) 	 * Add in the page array if there is one. The caller needs to set
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1997) 	 * rq_tailsz to PAGE_SIZE when the buffer has multiple pages and
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1998) 	 * ends at page boundary
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1999) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2000) 	remaining_data_length = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2001) 	for (i = 0; i < num_rqst; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2002) 		remaining_data_length += smb_rqst_len(server, &rqst_array[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2003) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2004) 	if (remaining_data_length > info->max_fragmented_send_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2005) 		log_write(ERR, "payload size %d > max size %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2006) 			remaining_data_length, info->max_fragmented_send_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2007) 		rc = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2008) 		goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2009) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2011) 	log_write(INFO, "num_rqst=%d total length=%u\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2012) 			num_rqst, remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2013) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2014) 	rqst_idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2015) next_rqst:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2016) 	rqst = &rqst_array[rqst_idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2017) 	iov = rqst->rq_iov;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2018) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2019) 	cifs_dbg(FYI, "Sending smb (RDMA): idx=%d smb_len=%lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2020) 		rqst_idx, smb_rqst_len(server, rqst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2021) 	for (i = 0; i < rqst->rq_nvec; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2022) 		dump_smb(iov[i].iov_base, iov[i].iov_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2023) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2025) 	log_write(INFO, "rqst_idx=%d nvec=%d rqst->rq_npages=%d rq_pagesz=%d rq_tailsz=%d buflen=%lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2026) 		  rqst_idx, rqst->rq_nvec, rqst->rq_npages, rqst->rq_pagesz,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2027) 		  rqst->rq_tailsz, smb_rqst_len(server, rqst));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2028) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2029) 	start = i = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2030) 	buflen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2031) 	while (true) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2032) 		buflen += iov[i].iov_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2033) 		if (buflen > max_iov_size) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2034) 			if (i > start) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2035) 				remaining_data_length -=
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2036) 					(buflen-iov[i].iov_len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2037) 				log_write(INFO, "sending iov[] from start=%d i=%d nvecs=%d remaining_data_length=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2038) 					  start, i, i - start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2039) 					  remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2040) 				rc = smbd_post_send_data(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2041) 					info, &iov[start], i-start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2042) 					remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2043) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2044) 					goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2045) 			} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2046) 				/* iov[start] is too big, break it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2047) 				nvecs = (buflen+max_iov_size-1)/max_iov_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2048) 				log_write(INFO, "iov[%d] iov_base=%p buflen=%d break to %d vectors\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2049) 					  start, iov[start].iov_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2050) 					  buflen, nvecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2051) 				for (j = 0; j < nvecs; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2052) 					vec.iov_base =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2053) 						(char *)iov[start].iov_base +
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2054) 						j*max_iov_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2055) 					vec.iov_len = max_iov_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2056) 					if (j == nvecs-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2057) 						vec.iov_len =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2058) 							buflen -
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2059) 							max_iov_size*(nvecs-1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2060) 					remaining_data_length -= vec.iov_len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2061) 					log_write(INFO,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2062) 						"sending vec j=%d iov_base=%p iov_len=%zu remaining_data_length=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2063) 						  j, vec.iov_base, vec.iov_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2064) 						  remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2065) 					rc = smbd_post_send_data(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2066) 						info, &vec, 1,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2067) 						remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2068) 					if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2069) 						goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2070) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2071) 				i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2072) 				if (i == rqst->rq_nvec)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2073) 					break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2074) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2075) 			start = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2076) 			buflen = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2077) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2078) 			i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2079) 			if (i == rqst->rq_nvec) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2080) 				/* send out all remaining vecs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2081) 				remaining_data_length -= buflen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2082) 				log_write(INFO, "sending iov[] from start=%d i=%d nvecs=%d remaining_data_length=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2083) 					  start, i, i - start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2084) 					  remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2085) 				rc = smbd_post_send_data(info, &iov[start],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2086) 					i-start, remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2087) 				if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2088) 					goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2089) 				break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2090) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2091) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2092) 		log_write(INFO, "looping i=%d buflen=%d\n", i, buflen);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2093) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2094) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2095) 	/* now sending pages if there are any */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2096) 	for (i = 0; i < rqst->rq_npages; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2097) 		unsigned int offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2098) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2099) 		rqst_page_get_length(rqst, i, &buflen, &offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2100) 		nvecs = (buflen + max_iov_size - 1) / max_iov_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2101) 		log_write(INFO, "sending pages buflen=%d nvecs=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2102) 			buflen, nvecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2103) 		for (j = 0; j < nvecs; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2104) 			size = max_iov_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2105) 			if (j == nvecs-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2106) 				size = buflen - j*max_iov_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2107) 			remaining_data_length -= size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2108) 			log_write(INFO, "sending pages i=%d offset=%d size=%d remaining_data_length=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2109) 				  i, j * max_iov_size + offset, size,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2110) 				  remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2111) 			rc = smbd_post_send_page(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2112) 				info, rqst->rq_pages[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2113) 				j*max_iov_size + offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2114) 				size, remaining_data_length);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2115) 			if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2116) 				goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2117) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2118) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2120) 	rqst_idx++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2121) 	if (rqst_idx < num_rqst)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2122) 		goto next_rqst;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2123) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2124) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2125) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2126) 	 * As an optimization, we don't wait for individual I/O to finish
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2127) 	 * before sending the next one.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2128) 	 * Send them all and wait for pending send count to get to 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2129) 	 * that means all the I/Os have been out and we are good to return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2130) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2132) 	wait_event(info->wait_send_pending,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2133) 		atomic_read(&info->send_pending) == 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2134) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2135) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2137) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2138) static void register_mr_done(struct ib_cq *cq, struct ib_wc *wc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2139) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2140) 	struct smbd_mr *mr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2141) 	struct ib_cqe *cqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2142) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2143) 	if (wc->status) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2144) 		log_rdma_mr(ERR, "status=%d\n", wc->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2145) 		cqe = wc->wr_cqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2146) 		mr = container_of(cqe, struct smbd_mr, cqe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2147) 		smbd_disconnect_rdma_connection(mr->conn);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2148) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2149) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2150) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2151) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2152)  * The work queue function that recovers MRs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2153)  * We need to call ib_dereg_mr() and ib_alloc_mr() before this MR can be used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2154)  * again. Both calls are slow, so finish them in a workqueue. This will not
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2155)  * block I/O path.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2156)  * There is one workqueue that recovers MRs, there is no need to lock as the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2157)  * I/O requests calling smbd_register_mr will never update the links in the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2158)  * mr_list.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2159)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2160) static void smbd_mr_recovery_work(struct work_struct *work)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2162) 	struct smbd_connection *info =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2163) 		container_of(work, struct smbd_connection, mr_recovery_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2164) 	struct smbd_mr *smbdirect_mr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2165) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2167) 	list_for_each_entry(smbdirect_mr, &info->mr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2168) 		if (smbdirect_mr->state == MR_ERROR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2170) 			/* recover this MR entry */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2171) 			rc = ib_dereg_mr(smbdirect_mr->mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2172) 			if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2173) 				log_rdma_mr(ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2174) 					"ib_dereg_mr failed rc=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2175) 					rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2176) 				smbd_disconnect_rdma_connection(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2177) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2178) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2180) 			smbdirect_mr->mr = ib_alloc_mr(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2181) 				info->pd, info->mr_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2182) 				info->max_frmr_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2183) 			if (IS_ERR(smbdirect_mr->mr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2184) 				log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x max_frmr_depth=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2185) 					    info->mr_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2186) 					    info->max_frmr_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2187) 				smbd_disconnect_rdma_connection(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2188) 				continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2189) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2190) 		} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2191) 			/* This MR is being used, don't recover it */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2192) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2193) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2194) 		smbdirect_mr->state = MR_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2196) 		/* smbdirect_mr->state is updated by this function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2197) 		 * and is read and updated by I/O issuing CPUs trying
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2198) 		 * to get a MR, the call to atomic_inc_return
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2199) 		 * implicates a memory barrier and guarantees this
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2200) 		 * value is updated before waking up any calls to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2201) 		 * get_mr() from the I/O issuing CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2202) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2203) 		if (atomic_inc_return(&info->mr_ready_count) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2204) 			wake_up_interruptible(&info->wait_mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2205) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2207) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2208) static void destroy_mr_list(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2209) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2210) 	struct smbd_mr *mr, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2212) 	cancel_work_sync(&info->mr_recovery_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2213) 	list_for_each_entry_safe(mr, tmp, &info->mr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2214) 		if (mr->state == MR_INVALIDATED)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2215) 			ib_dma_unmap_sg(info->id->device, mr->sgl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2216) 				mr->sgl_count, mr->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2217) 		ib_dereg_mr(mr->mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2218) 		kfree(mr->sgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2219) 		kfree(mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2220) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2222) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2223) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2224)  * Allocate MRs used for RDMA read/write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2225)  * The number of MRs will not exceed hardware capability in responder_resources
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2226)  * All MRs are kept in mr_list. The MR can be recovered after it's used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2227)  * Recovery is done in smbd_mr_recovery_work. The content of list entry changes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2228)  * as MRs are used and recovered for I/O, but the list links will not change
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2229)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2230) static int allocate_mr_list(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2231) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2232) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2233) 	struct smbd_mr *smbdirect_mr, *tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2234) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2235) 	INIT_LIST_HEAD(&info->mr_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2236) 	init_waitqueue_head(&info->wait_mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2237) 	spin_lock_init(&info->mr_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2238) 	atomic_set(&info->mr_ready_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2239) 	atomic_set(&info->mr_used_count, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2240) 	init_waitqueue_head(&info->wait_for_mr_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2241) 	/* Allocate more MRs (2x) than hardware responder_resources */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2242) 	for (i = 0; i < info->responder_resources * 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2243) 		smbdirect_mr = kzalloc(sizeof(*smbdirect_mr), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2244) 		if (!smbdirect_mr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2245) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2246) 		smbdirect_mr->mr = ib_alloc_mr(info->pd, info->mr_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2247) 					info->max_frmr_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2248) 		if (IS_ERR(smbdirect_mr->mr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2249) 			log_rdma_mr(ERR, "ib_alloc_mr failed mr_type=%x max_frmr_depth=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2250) 				    info->mr_type, info->max_frmr_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2251) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2252) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2253) 		smbdirect_mr->sgl = kcalloc(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2254) 					info->max_frmr_depth,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2255) 					sizeof(struct scatterlist),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2256) 					GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2257) 		if (!smbdirect_mr->sgl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2258) 			log_rdma_mr(ERR, "failed to allocate sgl\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2259) 			ib_dereg_mr(smbdirect_mr->mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2260) 			goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2261) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2262) 		smbdirect_mr->state = MR_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2263) 		smbdirect_mr->conn = info;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2265) 		list_add_tail(&smbdirect_mr->list, &info->mr_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2266) 		atomic_inc(&info->mr_ready_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2267) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2268) 	INIT_WORK(&info->mr_recovery_work, smbd_mr_recovery_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2269) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2270) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2271) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2272) 	kfree(smbdirect_mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2273) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2274) 	list_for_each_entry_safe(smbdirect_mr, tmp, &info->mr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2275) 		ib_dereg_mr(smbdirect_mr->mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2276) 		kfree(smbdirect_mr->sgl);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2277) 		kfree(smbdirect_mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2278) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2279) 	return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2280) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2282) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2283)  * Get a MR from mr_list. This function waits until there is at least one
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2284)  * MR available in the list. It may access the list while the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2285)  * smbd_mr_recovery_work is recovering the MR list. This doesn't need a lock
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2286)  * as they never modify the same places. However, there may be several CPUs
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2287)  * issueing I/O trying to get MR at the same time, mr_list_lock is used to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2288)  * protect this situation.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2289)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2290) static struct smbd_mr *get_mr(struct smbd_connection *info)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2291) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2292) 	struct smbd_mr *ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2293) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2294) again:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2295) 	rc = wait_event_interruptible(info->wait_mr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2296) 		atomic_read(&info->mr_ready_count) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2297) 		info->transport_status != SMBD_CONNECTED);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2298) 	if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2299) 		log_rdma_mr(ERR, "wait_event_interruptible rc=%x\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2300) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2301) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2303) 	if (info->transport_status != SMBD_CONNECTED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2304) 		log_rdma_mr(ERR, "info->transport_status=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2305) 			info->transport_status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2306) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2307) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2308) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2309) 	spin_lock(&info->mr_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2310) 	list_for_each_entry(ret, &info->mr_list, list) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2311) 		if (ret->state == MR_READY) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2312) 			ret->state = MR_REGISTERED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2313) 			spin_unlock(&info->mr_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2314) 			atomic_dec(&info->mr_ready_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2315) 			atomic_inc(&info->mr_used_count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2316) 			return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2317) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2318) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2319) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2320) 	spin_unlock(&info->mr_list_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2321) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2322) 	 * It is possible that we could fail to get MR because other processes may
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2323) 	 * try to acquire a MR at the same time. If this is the case, retry it.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2324) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2325) 	goto again;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2326) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2327) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2328) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2329)  * Register memory for RDMA read/write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2330)  * pages[]: the list of pages to register memory with
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2331)  * num_pages: the number of pages to register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2332)  * tailsz: if non-zero, the bytes to register in the last page
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2333)  * writing: true if this is a RDMA write (SMB read), false for RDMA read
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2334)  * need_invalidate: true if this MR needs to be locally invalidated after I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2335)  * return value: the MR registered, NULL if failed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2336)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2337) struct smbd_mr *smbd_register_mr(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2338) 	struct smbd_connection *info, struct page *pages[], int num_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2339) 	int offset, int tailsz, bool writing, bool need_invalidate)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2340) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2341) 	struct smbd_mr *smbdirect_mr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2342) 	int rc, i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2343) 	enum dma_data_direction dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2344) 	struct ib_reg_wr *reg_wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2345) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2346) 	if (num_pages > info->max_frmr_depth) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2347) 		log_rdma_mr(ERR, "num_pages=%d max_frmr_depth=%d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2348) 			num_pages, info->max_frmr_depth);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2349) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2350) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2351) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2352) 	smbdirect_mr = get_mr(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2353) 	if (!smbdirect_mr) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2354) 		log_rdma_mr(ERR, "get_mr returning NULL\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2355) 		return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2356) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2357) 	smbdirect_mr->need_invalidate = need_invalidate;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2358) 	smbdirect_mr->sgl_count = num_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2359) 	sg_init_table(smbdirect_mr->sgl, num_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2360) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2361) 	log_rdma_mr(INFO, "num_pages=0x%x offset=0x%x tailsz=0x%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2362) 			num_pages, offset, tailsz);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2363) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2364) 	if (num_pages == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2365) 		sg_set_page(&smbdirect_mr->sgl[0], pages[0], tailsz, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2366) 		goto skip_multiple_pages;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2367) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2369) 	/* We have at least two pages to register */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2370) 	sg_set_page(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2371) 		&smbdirect_mr->sgl[0], pages[0], PAGE_SIZE - offset, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2372) 	i = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2373) 	while (i < num_pages - 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2374) 		sg_set_page(&smbdirect_mr->sgl[i], pages[i], PAGE_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2375) 		i++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2376) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2377) 	sg_set_page(&smbdirect_mr->sgl[i], pages[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2378) 		tailsz ? tailsz : PAGE_SIZE, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2379) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2380) skip_multiple_pages:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2381) 	dir = writing ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2382) 	smbdirect_mr->dir = dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2383) 	rc = ib_dma_map_sg(info->id->device, smbdirect_mr->sgl, num_pages, dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2384) 	if (!rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2385) 		log_rdma_mr(ERR, "ib_dma_map_sg num_pages=%x dir=%x rc=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2386) 			num_pages, dir, rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2387) 		goto dma_map_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2388) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2390) 	rc = ib_map_mr_sg(smbdirect_mr->mr, smbdirect_mr->sgl, num_pages,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2391) 		NULL, PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2392) 	if (rc != num_pages) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2393) 		log_rdma_mr(ERR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2394) 			"ib_map_mr_sg failed rc = %d num_pages = %x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2395) 			rc, num_pages);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2396) 		goto map_mr_error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2397) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2398) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2399) 	ib_update_fast_reg_key(smbdirect_mr->mr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2400) 		ib_inc_rkey(smbdirect_mr->mr->rkey));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2401) 	reg_wr = &smbdirect_mr->wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2402) 	reg_wr->wr.opcode = IB_WR_REG_MR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2403) 	smbdirect_mr->cqe.done = register_mr_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2404) 	reg_wr->wr.wr_cqe = &smbdirect_mr->cqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2405) 	reg_wr->wr.num_sge = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2406) 	reg_wr->wr.send_flags = IB_SEND_SIGNALED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2407) 	reg_wr->mr = smbdirect_mr->mr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2408) 	reg_wr->key = smbdirect_mr->mr->rkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2409) 	reg_wr->access = writing ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2410) 			IB_ACCESS_REMOTE_WRITE | IB_ACCESS_LOCAL_WRITE :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2411) 			IB_ACCESS_REMOTE_READ;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2412) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2413) 	/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2414) 	 * There is no need for waiting for complemtion on ib_post_send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2415) 	 * on IB_WR_REG_MR. Hardware enforces a barrier and order of execution
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2416) 	 * on the next ib_post_send when we actaully send I/O to remote peer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2417) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2418) 	rc = ib_post_send(info->id->qp, &reg_wr->wr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2419) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2420) 		return smbdirect_mr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2422) 	log_rdma_mr(ERR, "ib_post_send failed rc=%x reg_wr->key=%x\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2423) 		rc, reg_wr->key);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2424) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2425) 	/* If all failed, attempt to recover this MR by setting it MR_ERROR*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2426) map_mr_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2427) 	ib_dma_unmap_sg(info->id->device, smbdirect_mr->sgl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2428) 		smbdirect_mr->sgl_count, smbdirect_mr->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2429) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2430) dma_map_error:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2431) 	smbdirect_mr->state = MR_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2432) 	if (atomic_dec_and_test(&info->mr_used_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2433) 		wake_up(&info->wait_for_mr_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2434) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2435) 	smbd_disconnect_rdma_connection(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2436) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2437) 	return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2438) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2439) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2440) static void local_inv_done(struct ib_cq *cq, struct ib_wc *wc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2441) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2442) 	struct smbd_mr *smbdirect_mr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2443) 	struct ib_cqe *cqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2444) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2445) 	cqe = wc->wr_cqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2446) 	smbdirect_mr = container_of(cqe, struct smbd_mr, cqe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2447) 	smbdirect_mr->state = MR_INVALIDATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2448) 	if (wc->status != IB_WC_SUCCESS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2449) 		log_rdma_mr(ERR, "invalidate failed status=%x\n", wc->status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2450) 		smbdirect_mr->state = MR_ERROR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2451) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2452) 	complete(&smbdirect_mr->invalidate_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2453) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2454) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2455) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2456)  * Deregister a MR after I/O is done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2457)  * This function may wait if remote invalidation is not used
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2458)  * and we have to locally invalidate the buffer to prevent data is being
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2459)  * modified by remote peer after upper layer consumes it
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2460)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2461) int smbd_deregister_mr(struct smbd_mr *smbdirect_mr)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2462) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2463) 	struct ib_send_wr *wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2464) 	struct smbd_connection *info = smbdirect_mr->conn;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2465) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2466) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2467) 	if (smbdirect_mr->need_invalidate) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2468) 		/* Need to finish local invalidation before returning */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2469) 		wr = &smbdirect_mr->inv_wr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2470) 		wr->opcode = IB_WR_LOCAL_INV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2471) 		smbdirect_mr->cqe.done = local_inv_done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2472) 		wr->wr_cqe = &smbdirect_mr->cqe;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2473) 		wr->num_sge = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2474) 		wr->ex.invalidate_rkey = smbdirect_mr->mr->rkey;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2475) 		wr->send_flags = IB_SEND_SIGNALED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2476) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2477) 		init_completion(&smbdirect_mr->invalidate_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2478) 		rc = ib_post_send(info->id->qp, wr, NULL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2479) 		if (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2480) 			log_rdma_mr(ERR, "ib_post_send failed rc=%x\n", rc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2481) 			smbd_disconnect_rdma_connection(info);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2482) 			goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2483) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2484) 		wait_for_completion(&smbdirect_mr->invalidate_done);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2485) 		smbdirect_mr->need_invalidate = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2486) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2487) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2488) 		 * For remote invalidation, just set it to MR_INVALIDATED
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2489) 		 * and defer to mr_recovery_work to recover the MR for next use
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2490) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2491) 		smbdirect_mr->state = MR_INVALIDATED;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2492) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2493) 	if (smbdirect_mr->state == MR_INVALIDATED) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2494) 		ib_dma_unmap_sg(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2495) 			info->id->device, smbdirect_mr->sgl,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2496) 			smbdirect_mr->sgl_count,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2497) 			smbdirect_mr->dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2498) 		smbdirect_mr->state = MR_READY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2499) 		if (atomic_inc_return(&info->mr_ready_count) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2500) 			wake_up_interruptible(&info->wait_mr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2501) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2502) 		/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2503) 		 * Schedule the work to do MR recovery for future I/Os MR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2504) 		 * recovery is slow and don't want it to block current I/O
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2505) 		 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2506) 		queue_work(info->workqueue, &info->mr_recovery_work);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2507) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2508) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2509) 	if (atomic_dec_and_test(&info->mr_used_count))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2510) 		wake_up(&info->wait_for_mr_cleanup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2511) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2512) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2513) }