]>
Commit | Line | Data |
---|---|---|
aef9ec39 RD |
1 | /* |
2 | * Copyright (c) 2005 Cisco Systems. All rights reserved. | |
3 | * | |
4 | * This software is available to you under a choice of one of two | |
5 | * licenses. You may choose to be licensed under the terms of the GNU | |
6 | * General Public License (GPL) Version 2, available from the file | |
7 | * COPYING in the main directory of this source tree, or the | |
8 | * OpenIB.org BSD license below: | |
9 | * | |
10 | * Redistribution and use in source and binary forms, with or | |
11 | * without modification, are permitted provided that the following | |
12 | * conditions are met: | |
13 | * | |
14 | * - Redistributions of source code must retain the above | |
15 | * copyright notice, this list of conditions and the following | |
16 | * disclaimer. | |
17 | * | |
18 | * - Redistributions in binary form must reproduce the above | |
19 | * copyright notice, this list of conditions and the following | |
20 | * disclaimer in the documentation and/or other materials | |
21 | * provided with the distribution. | |
22 | * | |
23 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | |
24 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | |
25 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | |
26 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS | |
27 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN | |
28 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN | |
29 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
30 | * SOFTWARE. | |
aef9ec39 RD |
31 | */ |
32 | ||
e0bda7d8 BVA |
33 | #define pr_fmt(fmt) PFX fmt |
34 | ||
aef9ec39 RD |
35 | #include <linux/module.h> |
36 | #include <linux/init.h> | |
37 | #include <linux/slab.h> | |
38 | #include <linux/err.h> | |
39 | #include <linux/string.h> | |
40 | #include <linux/parser.h> | |
41 | #include <linux/random.h> | |
de25968c | 42 | #include <linux/jiffies.h> |
aef9ec39 | 43 | |
60063497 | 44 | #include <linux/atomic.h> |
aef9ec39 RD |
45 | |
46 | #include <scsi/scsi.h> | |
47 | #include <scsi/scsi_device.h> | |
48 | #include <scsi/scsi_dbg.h> | |
49 | #include <scsi/srp.h> | |
3236822b | 50 | #include <scsi/scsi_transport_srp.h> |
aef9ec39 | 51 | |
aef9ec39 RD |
52 | #include "ib_srp.h" |
53 | ||
54 | #define DRV_NAME "ib_srp" | |
55 | #define PFX DRV_NAME ": " | |
e8ca4135 VP |
56 | #define DRV_VERSION "1.0" |
57 | #define DRV_RELDATE "July 1, 2013" | |
aef9ec39 RD |
58 | |
59 | MODULE_AUTHOR("Roland Dreier"); | |
60 | MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator " | |
61 | "v" DRV_VERSION " (" DRV_RELDATE ")"); | |
62 | MODULE_LICENSE("Dual BSD/GPL"); | |
63 | ||
49248644 DD |
64 | static unsigned int srp_sg_tablesize; |
65 | static unsigned int cmd_sg_entries; | |
c07d424d DD |
66 | static unsigned int indirect_sg_entries; |
67 | static bool allow_ext_sg; | |
49248644 | 68 | static int topspin_workarounds = 1; |
74b0a15b | 69 | |
49248644 DD |
70 | module_param(srp_sg_tablesize, uint, 0444); |
71 | MODULE_PARM_DESC(srp_sg_tablesize, "Deprecated name for cmd_sg_entries"); | |
74b0a15b | 72 | |
49248644 DD |
73 | module_param(cmd_sg_entries, uint, 0444); |
74 | MODULE_PARM_DESC(cmd_sg_entries, | |
75 | "Default number of gather/scatter entries in the SRP command (default is 12, max 255)"); | |
aef9ec39 | 76 | |
c07d424d DD |
77 | module_param(indirect_sg_entries, uint, 0444); |
78 | MODULE_PARM_DESC(indirect_sg_entries, | |
79 | "Default max number of gather/scatter entries (default is 12, max is " __stringify(SCSI_MAX_SG_CHAIN_SEGMENTS) ")"); | |
80 | ||
81 | module_param(allow_ext_sg, bool, 0444); | |
82 | MODULE_PARM_DESC(allow_ext_sg, | |
83 | "Default behavior when there are more than cmd_sg_entries S/G entries after mapping; fails the request when false (default false)"); | |
84 | ||
aef9ec39 RD |
85 | module_param(topspin_workarounds, int, 0444); |
86 | MODULE_PARM_DESC(topspin_workarounds, | |
87 | "Enable workarounds for Topspin/Cisco SRP target bugs if != 0"); | |
88 | ||
aef9ec39 RD |
89 | static void srp_add_one(struct ib_device *device); |
90 | static void srp_remove_one(struct ib_device *device); | |
9c03dc9f BVA |
91 | static void srp_recv_completion(struct ib_cq *cq, void *target_ptr); |
92 | static void srp_send_completion(struct ib_cq *cq, void *target_ptr); | |
aef9ec39 RD |
93 | static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event); |
94 | ||
3236822b FT |
95 | static struct scsi_transport_template *ib_srp_transport_template; |
96 | ||
aef9ec39 RD |
97 | static struct ib_client srp_client = { |
98 | .name = "srp", | |
99 | .add = srp_add_one, | |
100 | .remove = srp_remove_one | |
101 | }; | |
102 | ||
c1a0b23b MT |
103 | static struct ib_sa_client srp_sa_client; |
104 | ||
aef9ec39 RD |
105 | static inline struct srp_target_port *host_to_target(struct Scsi_Host *host) |
106 | { | |
107 | return (struct srp_target_port *) host->hostdata; | |
108 | } | |
109 | ||
110 | static const char *srp_target_info(struct Scsi_Host *host) | |
111 | { | |
112 | return host_to_target(host)->target_name; | |
113 | } | |
114 | ||
5d7cbfd6 RD |
115 | static int srp_target_is_topspin(struct srp_target_port *target) |
116 | { | |
117 | static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad }; | |
3d1ff48d | 118 | static const u8 cisco_oui[3] = { 0x00, 0x1b, 0x0d }; |
5d7cbfd6 RD |
119 | |
120 | return topspin_workarounds && | |
3d1ff48d RK |
121 | (!memcmp(&target->ioc_guid, topspin_oui, sizeof topspin_oui) || |
122 | !memcmp(&target->ioc_guid, cisco_oui, sizeof cisco_oui)); | |
5d7cbfd6 RD |
123 | } |
124 | ||
aef9ec39 RD |
125 | static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size, |
126 | gfp_t gfp_mask, | |
127 | enum dma_data_direction direction) | |
128 | { | |
129 | struct srp_iu *iu; | |
130 | ||
131 | iu = kmalloc(sizeof *iu, gfp_mask); | |
132 | if (!iu) | |
133 | goto out; | |
134 | ||
135 | iu->buf = kzalloc(size, gfp_mask); | |
136 | if (!iu->buf) | |
137 | goto out_free_iu; | |
138 | ||
05321937 GKH |
139 | iu->dma = ib_dma_map_single(host->srp_dev->dev, iu->buf, size, |
140 | direction); | |
141 | if (ib_dma_mapping_error(host->srp_dev->dev, iu->dma)) | |
aef9ec39 RD |
142 | goto out_free_buf; |
143 | ||
144 | iu->size = size; | |
145 | iu->direction = direction; | |
146 | ||
147 | return iu; | |
148 | ||
149 | out_free_buf: | |
150 | kfree(iu->buf); | |
151 | out_free_iu: | |
152 | kfree(iu); | |
153 | out: | |
154 | return NULL; | |
155 | } | |
156 | ||
157 | static void srp_free_iu(struct srp_host *host, struct srp_iu *iu) | |
158 | { | |
159 | if (!iu) | |
160 | return; | |
161 | ||
05321937 GKH |
162 | ib_dma_unmap_single(host->srp_dev->dev, iu->dma, iu->size, |
163 | iu->direction); | |
aef9ec39 RD |
164 | kfree(iu->buf); |
165 | kfree(iu); | |
166 | } | |
167 | ||
168 | static void srp_qp_event(struct ib_event *event, void *context) | |
169 | { | |
e0bda7d8 | 170 | pr_debug("QP event %d\n", event->event); |
aef9ec39 RD |
171 | } |
172 | ||
173 | static int srp_init_qp(struct srp_target_port *target, | |
174 | struct ib_qp *qp) | |
175 | { | |
176 | struct ib_qp_attr *attr; | |
177 | int ret; | |
178 | ||
179 | attr = kmalloc(sizeof *attr, GFP_KERNEL); | |
180 | if (!attr) | |
181 | return -ENOMEM; | |
182 | ||
969a60f9 RD |
183 | ret = ib_find_pkey(target->srp_host->srp_dev->dev, |
184 | target->srp_host->port, | |
185 | be16_to_cpu(target->path.pkey), | |
186 | &attr->pkey_index); | |
aef9ec39 RD |
187 | if (ret) |
188 | goto out; | |
189 | ||
190 | attr->qp_state = IB_QPS_INIT; | |
191 | attr->qp_access_flags = (IB_ACCESS_REMOTE_READ | | |
192 | IB_ACCESS_REMOTE_WRITE); | |
193 | attr->port_num = target->srp_host->port; | |
194 | ||
195 | ret = ib_modify_qp(qp, attr, | |
196 | IB_QP_STATE | | |
197 | IB_QP_PKEY_INDEX | | |
198 | IB_QP_ACCESS_FLAGS | | |
199 | IB_QP_PORT); | |
200 | ||
201 | out: | |
202 | kfree(attr); | |
203 | return ret; | |
204 | } | |
205 | ||
9fe4bcf4 DD |
206 | static int srp_new_cm_id(struct srp_target_port *target) |
207 | { | |
208 | struct ib_cm_id *new_cm_id; | |
209 | ||
05321937 | 210 | new_cm_id = ib_create_cm_id(target->srp_host->srp_dev->dev, |
9fe4bcf4 DD |
211 | srp_cm_handler, target); |
212 | if (IS_ERR(new_cm_id)) | |
213 | return PTR_ERR(new_cm_id); | |
214 | ||
215 | if (target->cm_id) | |
216 | ib_destroy_cm_id(target->cm_id); | |
217 | target->cm_id = new_cm_id; | |
218 | ||
219 | return 0; | |
220 | } | |
221 | ||
aef9ec39 RD |
222 | static int srp_create_target_ib(struct srp_target_port *target) |
223 | { | |
224 | struct ib_qp_init_attr *init_attr; | |
73aa89ed IR |
225 | struct ib_cq *recv_cq, *send_cq; |
226 | struct ib_qp *qp; | |
aef9ec39 RD |
227 | int ret; |
228 | ||
229 | init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL); | |
230 | if (!init_attr) | |
231 | return -ENOMEM; | |
232 | ||
73aa89ed | 233 | recv_cq = ib_create_cq(target->srp_host->srp_dev->dev, |
4b5e5f41 BVA |
234 | srp_recv_completion, NULL, target, SRP_RQ_SIZE, |
235 | target->comp_vector); | |
73aa89ed IR |
236 | if (IS_ERR(recv_cq)) { |
237 | ret = PTR_ERR(recv_cq); | |
da9d2f07 | 238 | goto err; |
aef9ec39 RD |
239 | } |
240 | ||
73aa89ed | 241 | send_cq = ib_create_cq(target->srp_host->srp_dev->dev, |
4b5e5f41 BVA |
242 | srp_send_completion, NULL, target, SRP_SQ_SIZE, |
243 | target->comp_vector); | |
73aa89ed IR |
244 | if (IS_ERR(send_cq)) { |
245 | ret = PTR_ERR(send_cq); | |
da9d2f07 | 246 | goto err_recv_cq; |
9c03dc9f BVA |
247 | } |
248 | ||
73aa89ed | 249 | ib_req_notify_cq(recv_cq, IB_CQ_NEXT_COMP); |
aef9ec39 RD |
250 | |
251 | init_attr->event_handler = srp_qp_event; | |
252 | init_attr->cap.max_send_wr = SRP_SQ_SIZE; | |
253 | init_attr->cap.max_recv_wr = SRP_RQ_SIZE; | |
254 | init_attr->cap.max_recv_sge = 1; | |
255 | init_attr->cap.max_send_sge = 1; | |
256 | init_attr->sq_sig_type = IB_SIGNAL_ALL_WR; | |
257 | init_attr->qp_type = IB_QPT_RC; | |
73aa89ed IR |
258 | init_attr->send_cq = send_cq; |
259 | init_attr->recv_cq = recv_cq; | |
aef9ec39 | 260 | |
73aa89ed IR |
261 | qp = ib_create_qp(target->srp_host->srp_dev->pd, init_attr); |
262 | if (IS_ERR(qp)) { | |
263 | ret = PTR_ERR(qp); | |
da9d2f07 | 264 | goto err_send_cq; |
aef9ec39 RD |
265 | } |
266 | ||
73aa89ed | 267 | ret = srp_init_qp(target, qp); |
da9d2f07 RD |
268 | if (ret) |
269 | goto err_qp; | |
aef9ec39 | 270 | |
73aa89ed IR |
271 | if (target->qp) |
272 | ib_destroy_qp(target->qp); | |
273 | if (target->recv_cq) | |
274 | ib_destroy_cq(target->recv_cq); | |
275 | if (target->send_cq) | |
276 | ib_destroy_cq(target->send_cq); | |
277 | ||
278 | target->qp = qp; | |
279 | target->recv_cq = recv_cq; | |
280 | target->send_cq = send_cq; | |
281 | ||
da9d2f07 RD |
282 | kfree(init_attr); |
283 | return 0; | |
284 | ||
285 | err_qp: | |
73aa89ed | 286 | ib_destroy_qp(qp); |
da9d2f07 RD |
287 | |
288 | err_send_cq: | |
73aa89ed | 289 | ib_destroy_cq(send_cq); |
da9d2f07 RD |
290 | |
291 | err_recv_cq: | |
73aa89ed | 292 | ib_destroy_cq(recv_cq); |
da9d2f07 RD |
293 | |
294 | err: | |
aef9ec39 RD |
295 | kfree(init_attr); |
296 | return ret; | |
297 | } | |
298 | ||
299 | static void srp_free_target_ib(struct srp_target_port *target) | |
300 | { | |
301 | int i; | |
302 | ||
303 | ib_destroy_qp(target->qp); | |
9c03dc9f BVA |
304 | ib_destroy_cq(target->send_cq); |
305 | ib_destroy_cq(target->recv_cq); | |
aef9ec39 | 306 | |
73aa89ed IR |
307 | target->qp = NULL; |
308 | target->send_cq = target->recv_cq = NULL; | |
309 | ||
aef9ec39 RD |
310 | for (i = 0; i < SRP_RQ_SIZE; ++i) |
311 | srp_free_iu(target->srp_host, target->rx_ring[i]); | |
dd5e6e38 | 312 | for (i = 0; i < SRP_SQ_SIZE; ++i) |
aef9ec39 RD |
313 | srp_free_iu(target->srp_host, target->tx_ring[i]); |
314 | } | |
315 | ||
316 | static void srp_path_rec_completion(int status, | |
317 | struct ib_sa_path_rec *pathrec, | |
318 | void *target_ptr) | |
319 | { | |
320 | struct srp_target_port *target = target_ptr; | |
321 | ||
322 | target->status = status; | |
323 | if (status) | |
7aa54bd7 DD |
324 | shost_printk(KERN_ERR, target->scsi_host, |
325 | PFX "Got failed path rec status %d\n", status); | |
aef9ec39 RD |
326 | else |
327 | target->path = *pathrec; | |
328 | complete(&target->done); | |
329 | } | |
330 | ||
331 | static int srp_lookup_path(struct srp_target_port *target) | |
332 | { | |
333 | target->path.numb_path = 1; | |
334 | ||
335 | init_completion(&target->done); | |
336 | ||
c1a0b23b | 337 | target->path_query_id = ib_sa_path_rec_get(&srp_sa_client, |
05321937 | 338 | target->srp_host->srp_dev->dev, |
aef9ec39 RD |
339 | target->srp_host->port, |
340 | &target->path, | |
247e020e | 341 | IB_SA_PATH_REC_SERVICE_ID | |
aef9ec39 RD |
342 | IB_SA_PATH_REC_DGID | |
343 | IB_SA_PATH_REC_SGID | | |
344 | IB_SA_PATH_REC_NUMB_PATH | | |
345 | IB_SA_PATH_REC_PKEY, | |
346 | SRP_PATH_REC_TIMEOUT_MS, | |
347 | GFP_KERNEL, | |
348 | srp_path_rec_completion, | |
349 | target, &target->path_query); | |
350 | if (target->path_query_id < 0) | |
351 | return target->path_query_id; | |
352 | ||
353 | wait_for_completion(&target->done); | |
354 | ||
355 | if (target->status < 0) | |
7aa54bd7 DD |
356 | shost_printk(KERN_WARNING, target->scsi_host, |
357 | PFX "Path record query failed\n"); | |
aef9ec39 RD |
358 | |
359 | return target->status; | |
360 | } | |
361 | ||
362 | static int srp_send_req(struct srp_target_port *target) | |
363 | { | |
364 | struct { | |
365 | struct ib_cm_req_param param; | |
366 | struct srp_login_req priv; | |
367 | } *req = NULL; | |
368 | int status; | |
369 | ||
370 | req = kzalloc(sizeof *req, GFP_KERNEL); | |
371 | if (!req) | |
372 | return -ENOMEM; | |
373 | ||
374 | req->param.primary_path = &target->path; | |
375 | req->param.alternate_path = NULL; | |
376 | req->param.service_id = target->service_id; | |
377 | req->param.qp_num = target->qp->qp_num; | |
378 | req->param.qp_type = target->qp->qp_type; | |
379 | req->param.private_data = &req->priv; | |
380 | req->param.private_data_len = sizeof req->priv; | |
381 | req->param.flow_control = 1; | |
382 | ||
383 | get_random_bytes(&req->param.starting_psn, 4); | |
384 | req->param.starting_psn &= 0xffffff; | |
385 | ||
386 | /* | |
387 | * Pick some arbitrary defaults here; we could make these | |
388 | * module parameters if anyone cared about setting them. | |
389 | */ | |
390 | req->param.responder_resources = 4; | |
391 | req->param.remote_cm_response_timeout = 20; | |
392 | req->param.local_cm_response_timeout = 20; | |
393 | req->param.retry_count = 7; | |
394 | req->param.rnr_retry_count = 7; | |
395 | req->param.max_cm_retries = 15; | |
396 | ||
397 | req->priv.opcode = SRP_LOGIN_REQ; | |
398 | req->priv.tag = 0; | |
49248644 | 399 | req->priv.req_it_iu_len = cpu_to_be32(target->max_iu_len); |
aef9ec39 RD |
400 | req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT | |
401 | SRP_BUF_FORMAT_INDIRECT); | |
0c0450db | 402 | /* |
3cd96564 | 403 | * In the published SRP specification (draft rev. 16a), the |
0c0450db R |
404 | * port identifier format is 8 bytes of ID extension followed |
405 | * by 8 bytes of GUID. Older drafts put the two halves in the | |
406 | * opposite order, so that the GUID comes first. | |
407 | * | |
408 | * Targets conforming to these obsolete drafts can be | |
409 | * recognized by the I/O Class they report. | |
410 | */ | |
411 | if (target->io_class == SRP_REV10_IB_IO_CLASS) { | |
412 | memcpy(req->priv.initiator_port_id, | |
01cb9bcb | 413 | &target->path.sgid.global.interface_id, 8); |
0c0450db | 414 | memcpy(req->priv.initiator_port_id + 8, |
01cb9bcb | 415 | &target->initiator_ext, 8); |
0c0450db R |
416 | memcpy(req->priv.target_port_id, &target->ioc_guid, 8); |
417 | memcpy(req->priv.target_port_id + 8, &target->id_ext, 8); | |
418 | } else { | |
419 | memcpy(req->priv.initiator_port_id, | |
01cb9bcb IR |
420 | &target->initiator_ext, 8); |
421 | memcpy(req->priv.initiator_port_id + 8, | |
422 | &target->path.sgid.global.interface_id, 8); | |
0c0450db R |
423 | memcpy(req->priv.target_port_id, &target->id_ext, 8); |
424 | memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8); | |
425 | } | |
426 | ||
aef9ec39 RD |
427 | /* |
428 | * Topspin/Cisco SRP targets will reject our login unless we | |
01cb9bcb IR |
429 | * zero out the first 8 bytes of our initiator port ID and set |
430 | * the second 8 bytes to the local node GUID. | |
aef9ec39 | 431 | */ |
5d7cbfd6 | 432 | if (srp_target_is_topspin(target)) { |
7aa54bd7 DD |
433 | shost_printk(KERN_DEBUG, target->scsi_host, |
434 | PFX "Topspin/Cisco initiator port ID workaround " | |
435 | "activated for target GUID %016llx\n", | |
436 | (unsigned long long) be64_to_cpu(target->ioc_guid)); | |
aef9ec39 | 437 | memset(req->priv.initiator_port_id, 0, 8); |
01cb9bcb | 438 | memcpy(req->priv.initiator_port_id + 8, |
05321937 | 439 | &target->srp_host->srp_dev->dev->node_guid, 8); |
aef9ec39 | 440 | } |
aef9ec39 RD |
441 | |
442 | status = ib_send_cm_req(target->cm_id, &req->param); | |
443 | ||
444 | kfree(req); | |
445 | ||
446 | return status; | |
447 | } | |
448 | ||
ef6c49d8 BVA |
449 | static bool srp_queue_remove_work(struct srp_target_port *target) |
450 | { | |
451 | bool changed = false; | |
452 | ||
453 | spin_lock_irq(&target->lock); | |
454 | if (target->state != SRP_TARGET_REMOVED) { | |
455 | target->state = SRP_TARGET_REMOVED; | |
456 | changed = true; | |
457 | } | |
458 | spin_unlock_irq(&target->lock); | |
459 | ||
460 | if (changed) | |
461 | queue_work(system_long_wq, &target->remove_work); | |
462 | ||
463 | return changed; | |
464 | } | |
465 | ||
294c875a BVA |
466 | static bool srp_change_conn_state(struct srp_target_port *target, |
467 | bool connected) | |
468 | { | |
469 | bool changed = false; | |
470 | ||
471 | spin_lock_irq(&target->lock); | |
472 | if (target->connected != connected) { | |
473 | target->connected = connected; | |
474 | changed = true; | |
475 | } | |
476 | spin_unlock_irq(&target->lock); | |
477 | ||
478 | return changed; | |
479 | } | |
480 | ||
aef9ec39 RD |
481 | static void srp_disconnect_target(struct srp_target_port *target) |
482 | { | |
294c875a BVA |
483 | if (srp_change_conn_state(target, false)) { |
484 | /* XXX should send SRP_I_LOGOUT request */ | |
aef9ec39 | 485 | |
294c875a BVA |
486 | if (ib_send_cm_dreq(target->cm_id, NULL, 0)) { |
487 | shost_printk(KERN_DEBUG, target->scsi_host, | |
488 | PFX "Sending CM DREQ failed\n"); | |
294c875a | 489 | } |
e6581056 | 490 | } |
aef9ec39 RD |
491 | } |
492 | ||
8f26c9ff DD |
493 | static void srp_free_req_data(struct srp_target_port *target) |
494 | { | |
c07d424d | 495 | struct ib_device *ibdev = target->srp_host->srp_dev->dev; |
8f26c9ff DD |
496 | struct srp_request *req; |
497 | int i; | |
498 | ||
499 | for (i = 0, req = target->req_ring; i < SRP_CMD_SQ_SIZE; ++i, ++req) { | |
500 | kfree(req->fmr_list); | |
501 | kfree(req->map_page); | |
c07d424d DD |
502 | if (req->indirect_dma_addr) { |
503 | ib_dma_unmap_single(ibdev, req->indirect_dma_addr, | |
504 | target->indirect_size, | |
505 | DMA_TO_DEVICE); | |
506 | } | |
507 | kfree(req->indirect_desc); | |
8f26c9ff DD |
508 | } |
509 | } | |
510 | ||
683b159a BVA |
511 | /** |
512 | * srp_del_scsi_host_attr() - Remove attributes defined in the host template. | |
513 | * @shost: SCSI host whose attributes to remove from sysfs. | |
514 | * | |
515 | * Note: Any attributes defined in the host template and that did not exist | |
516 | * before invocation of this function will be ignored. | |
517 | */ | |
518 | static void srp_del_scsi_host_attr(struct Scsi_Host *shost) | |
519 | { | |
520 | struct device_attribute **attr; | |
521 | ||
522 | for (attr = shost->hostt->shost_attrs; attr && *attr; ++attr) | |
523 | device_remove_file(&shost->shost_dev, *attr); | |
524 | } | |
525 | ||
ee12d6a8 BVA |
526 | static void srp_remove_target(struct srp_target_port *target) |
527 | { | |
ef6c49d8 BVA |
528 | WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED); |
529 | ||
ee12d6a8 BVA |
530 | srp_del_scsi_host_attr(target->scsi_host); |
531 | srp_remove_host(target->scsi_host); | |
532 | scsi_remove_host(target->scsi_host); | |
ef6c49d8 | 533 | srp_disconnect_target(target); |
ee12d6a8 BVA |
534 | ib_destroy_cm_id(target->cm_id); |
535 | srp_free_target_ib(target); | |
536 | srp_free_req_data(target); | |
537 | scsi_host_put(target->scsi_host); | |
538 | } | |
539 | ||
c4028958 | 540 | static void srp_remove_work(struct work_struct *work) |
aef9ec39 | 541 | { |
c4028958 | 542 | struct srp_target_port *target = |
ef6c49d8 | 543 | container_of(work, struct srp_target_port, remove_work); |
aef9ec39 | 544 | |
ef6c49d8 | 545 | WARN_ON_ONCE(target->state != SRP_TARGET_REMOVED); |
aef9ec39 | 546 | |
96fc248a BVA |
547 | srp_remove_target(target); |
548 | ||
b3589fd4 | 549 | spin_lock(&target->srp_host->target_lock); |
aef9ec39 | 550 | list_del(&target->list); |
b3589fd4 | 551 | spin_unlock(&target->srp_host->target_lock); |
aef9ec39 RD |
552 | } |
553 | ||
dc1bdbd9 BVA |
554 | static void srp_rport_delete(struct srp_rport *rport) |
555 | { | |
556 | struct srp_target_port *target = rport->lld_data; | |
557 | ||
558 | srp_queue_remove_work(target); | |
559 | } | |
560 | ||
aef9ec39 RD |
561 | static int srp_connect_target(struct srp_target_port *target) |
562 | { | |
9fe4bcf4 | 563 | int retries = 3; |
aef9ec39 RD |
564 | int ret; |
565 | ||
294c875a BVA |
566 | WARN_ON_ONCE(target->connected); |
567 | ||
948d1e88 BVA |
568 | target->qp_in_error = false; |
569 | ||
aef9ec39 RD |
570 | ret = srp_lookup_path(target); |
571 | if (ret) | |
572 | return ret; | |
573 | ||
574 | while (1) { | |
575 | init_completion(&target->done); | |
576 | ret = srp_send_req(target); | |
577 | if (ret) | |
578 | return ret; | |
579 | wait_for_completion(&target->done); | |
580 | ||
581 | /* | |
582 | * The CM event handling code will set status to | |
583 | * SRP_PORT_REDIRECT if we get a port redirect REJ | |
584 | * back, or SRP_DLID_REDIRECT if we get a lid/qp | |
585 | * redirect REJ back. | |
586 | */ | |
587 | switch (target->status) { | |
588 | case 0: | |
294c875a | 589 | srp_change_conn_state(target, true); |
aef9ec39 RD |
590 | return 0; |
591 | ||
592 | case SRP_PORT_REDIRECT: | |
593 | ret = srp_lookup_path(target); | |
594 | if (ret) | |
595 | return ret; | |
596 | break; | |
597 | ||
598 | case SRP_DLID_REDIRECT: | |
599 | break; | |
600 | ||
9fe4bcf4 DD |
601 | case SRP_STALE_CONN: |
602 | /* Our current CM id was stale, and is now in timewait. | |
603 | * Try to reconnect with a new one. | |
604 | */ | |
605 | if (!retries-- || srp_new_cm_id(target)) { | |
606 | shost_printk(KERN_ERR, target->scsi_host, PFX | |
607 | "giving up on stale connection\n"); | |
608 | target->status = -ECONNRESET; | |
609 | return target->status; | |
610 | } | |
611 | ||
612 | shost_printk(KERN_ERR, target->scsi_host, PFX | |
613 | "retrying stale connection\n"); | |
614 | break; | |
615 | ||
aef9ec39 RD |
616 | default: |
617 | return target->status; | |
618 | } | |
619 | } | |
620 | } | |
621 | ||
d945e1df RD |
622 | static void srp_unmap_data(struct scsi_cmnd *scmnd, |
623 | struct srp_target_port *target, | |
624 | struct srp_request *req) | |
625 | { | |
8f26c9ff DD |
626 | struct ib_device *ibdev = target->srp_host->srp_dev->dev; |
627 | struct ib_pool_fmr **pfmr; | |
628 | ||
bb350d1d | 629 | if (!scsi_sglist(scmnd) || |
d945e1df RD |
630 | (scmnd->sc_data_direction != DMA_TO_DEVICE && |
631 | scmnd->sc_data_direction != DMA_FROM_DEVICE)) | |
632 | return; | |
633 | ||
8f26c9ff DD |
634 | pfmr = req->fmr_list; |
635 | while (req->nfmr--) | |
636 | ib_fmr_pool_unmap(*pfmr++); | |
f5358a17 | 637 | |
8f26c9ff DD |
638 | ib_dma_unmap_sg(ibdev, scsi_sglist(scmnd), scsi_sg_count(scmnd), |
639 | scmnd->sc_data_direction); | |
d945e1df RD |
640 | } |
641 | ||
22032991 BVA |
642 | /** |
643 | * srp_claim_req - Take ownership of the scmnd associated with a request. | |
644 | * @target: SRP target port. | |
645 | * @req: SRP request. | |
646 | * @scmnd: If NULL, take ownership of @req->scmnd. If not NULL, only take | |
647 | * ownership of @req->scmnd if it equals @scmnd. | |
648 | * | |
649 | * Return value: | |
650 | * Either NULL or a pointer to the SCSI command the caller became owner of. | |
651 | */ | |
652 | static struct scsi_cmnd *srp_claim_req(struct srp_target_port *target, | |
653 | struct srp_request *req, | |
654 | struct scsi_cmnd *scmnd) | |
655 | { | |
656 | unsigned long flags; | |
657 | ||
658 | spin_lock_irqsave(&target->lock, flags); | |
659 | if (!scmnd) { | |
660 | scmnd = req->scmnd; | |
661 | req->scmnd = NULL; | |
662 | } else if (req->scmnd == scmnd) { | |
663 | req->scmnd = NULL; | |
664 | } else { | |
665 | scmnd = NULL; | |
666 | } | |
667 | spin_unlock_irqrestore(&target->lock, flags); | |
668 | ||
669 | return scmnd; | |
670 | } | |
671 | ||
672 | /** | |
673 | * srp_free_req() - Unmap data and add request to the free request list. | |
674 | */ | |
675 | static void srp_free_req(struct srp_target_port *target, | |
676 | struct srp_request *req, struct scsi_cmnd *scmnd, | |
677 | s32 req_lim_delta) | |
526b4caa | 678 | { |
94a9174c BVA |
679 | unsigned long flags; |
680 | ||
22032991 BVA |
681 | srp_unmap_data(scmnd, target, req); |
682 | ||
e9684678 | 683 | spin_lock_irqsave(&target->lock, flags); |
94a9174c | 684 | target->req_lim += req_lim_delta; |
536ae14e | 685 | list_add_tail(&req->list, &target->free_reqs); |
e9684678 | 686 | spin_unlock_irqrestore(&target->lock, flags); |
526b4caa IR |
687 | } |
688 | ||
689 | static void srp_reset_req(struct srp_target_port *target, struct srp_request *req) | |
690 | { | |
22032991 BVA |
691 | struct scsi_cmnd *scmnd = srp_claim_req(target, req, NULL); |
692 | ||
693 | if (scmnd) { | |
9b796d06 | 694 | srp_free_req(target, req, scmnd, 0); |
22032991 BVA |
695 | scmnd->result = DID_RESET << 16; |
696 | scmnd->scsi_done(scmnd); | |
22032991 | 697 | } |
526b4caa IR |
698 | } |
699 | ||
aef9ec39 RD |
700 | static int srp_reconnect_target(struct srp_target_port *target) |
701 | { | |
09be70a2 | 702 | struct Scsi_Host *shost = target->scsi_host; |
dcb4cb85 | 703 | int i, ret; |
aef9ec39 | 704 | |
09be70a2 BVA |
705 | scsi_target_block(&shost->shost_gendev); |
706 | ||
aef9ec39 RD |
707 | srp_disconnect_target(target); |
708 | /* | |
c7c4e7ff BVA |
709 | * Now get a new local CM ID so that we avoid confusing the target in |
710 | * case things are really fouled up. Doing so also ensures that all CM | |
711 | * callbacks will have finished before a new QP is allocated. | |
aef9ec39 | 712 | */ |
9fe4bcf4 | 713 | ret = srp_new_cm_id(target); |
c7c4e7ff BVA |
714 | /* |
715 | * Whether or not creating a new CM ID succeeded, create a new | |
716 | * QP. This guarantees that all completion callback function | |
717 | * invocations have finished before request resetting starts. | |
718 | */ | |
719 | if (ret == 0) | |
720 | ret = srp_create_target_ib(target); | |
721 | else | |
722 | srp_create_target_ib(target); | |
aef9ec39 | 723 | |
536ae14e BVA |
724 | for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { |
725 | struct srp_request *req = &target->req_ring[i]; | |
726 | if (req->scmnd) | |
727 | srp_reset_req(target, req); | |
728 | } | |
aef9ec39 | 729 | |
536ae14e | 730 | INIT_LIST_HEAD(&target->free_tx); |
dcb4cb85 | 731 | for (i = 0; i < SRP_SQ_SIZE; ++i) |
536ae14e | 732 | list_add(&target->tx_ring[i]->list, &target->free_tx); |
aef9ec39 | 733 | |
c7c4e7ff BVA |
734 | if (ret == 0) |
735 | ret = srp_connect_target(target); | |
09be70a2 | 736 | |
09be70a2 BVA |
737 | scsi_target_unblock(&shost->shost_gendev, ret == 0 ? SDEV_RUNNING : |
738 | SDEV_TRANSPORT_OFFLINE); | |
2ce19e72 | 739 | target->transport_offline = !!ret; |
09be70a2 | 740 | |
aef9ec39 RD |
741 | if (ret) |
742 | goto err; | |
743 | ||
09be70a2 | 744 | shost_printk(KERN_INFO, target->scsi_host, PFX "reconnect succeeded\n"); |
aef9ec39 RD |
745 | |
746 | return ret; | |
747 | ||
748 | err: | |
7aa54bd7 DD |
749 | shost_printk(KERN_ERR, target->scsi_host, |
750 | PFX "reconnect failed (%d), removing target port.\n", ret); | |
aef9ec39 RD |
751 | |
752 | /* | |
753 | * We couldn't reconnect, so kill our target port off. | |
9709f0e0 BVA |
754 | * However, we have to defer the real removal because we |
755 | * are in the context of the SCSI error handler now, which | |
756 | * will deadlock if we call scsi_remove_host(). | |
aef9ec39 | 757 | */ |
ef6c49d8 | 758 | srp_queue_remove_work(target); |
aef9ec39 RD |
759 | |
760 | return ret; | |
761 | } | |
762 | ||
8f26c9ff DD |
763 | static void srp_map_desc(struct srp_map_state *state, dma_addr_t dma_addr, |
764 | unsigned int dma_len, u32 rkey) | |
f5358a17 | 765 | { |
8f26c9ff | 766 | struct srp_direct_buf *desc = state->desc; |
f5358a17 | 767 | |
8f26c9ff DD |
768 | desc->va = cpu_to_be64(dma_addr); |
769 | desc->key = cpu_to_be32(rkey); | |
770 | desc->len = cpu_to_be32(dma_len); | |
f5358a17 | 771 | |
8f26c9ff DD |
772 | state->total_len += dma_len; |
773 | state->desc++; | |
774 | state->ndesc++; | |
775 | } | |
559ce8f1 | 776 | |
8f26c9ff DD |
777 | static int srp_map_finish_fmr(struct srp_map_state *state, |
778 | struct srp_target_port *target) | |
779 | { | |
780 | struct srp_device *dev = target->srp_host->srp_dev; | |
781 | struct ib_pool_fmr *fmr; | |
782 | u64 io_addr = 0; | |
85507bcc | 783 | |
8f26c9ff DD |
784 | if (!state->npages) |
785 | return 0; | |
f5358a17 | 786 | |
8f26c9ff DD |
787 | if (state->npages == 1) { |
788 | srp_map_desc(state, state->base_dma_addr, state->fmr_len, | |
789 | target->rkey); | |
790 | state->npages = state->fmr_len = 0; | |
791 | return 0; | |
f5358a17 RD |
792 | } |
793 | ||
8f26c9ff DD |
794 | fmr = ib_fmr_pool_map_phys(dev->fmr_pool, state->pages, |
795 | state->npages, io_addr); | |
796 | if (IS_ERR(fmr)) | |
797 | return PTR_ERR(fmr); | |
f5358a17 | 798 | |
8f26c9ff DD |
799 | *state->next_fmr++ = fmr; |
800 | state->nfmr++; | |
f5358a17 | 801 | |
8f26c9ff DD |
802 | srp_map_desc(state, 0, state->fmr_len, fmr->fmr->rkey); |
803 | state->npages = state->fmr_len = 0; | |
804 | return 0; | |
805 | } | |
806 | ||
807 | static void srp_map_update_start(struct srp_map_state *state, | |
808 | struct scatterlist *sg, int sg_index, | |
809 | dma_addr_t dma_addr) | |
810 | { | |
811 | state->unmapped_sg = sg; | |
812 | state->unmapped_index = sg_index; | |
813 | state->unmapped_addr = dma_addr; | |
814 | } | |
85507bcc | 815 | |
8f26c9ff DD |
816 | static int srp_map_sg_entry(struct srp_map_state *state, |
817 | struct srp_target_port *target, | |
818 | struct scatterlist *sg, int sg_index, | |
819 | int use_fmr) | |
820 | { | |
821 | struct srp_device *dev = target->srp_host->srp_dev; | |
822 | struct ib_device *ibdev = dev->dev; | |
823 | dma_addr_t dma_addr = ib_sg_dma_address(ibdev, sg); | |
824 | unsigned int dma_len = ib_sg_dma_len(ibdev, sg); | |
825 | unsigned int len; | |
826 | int ret; | |
827 | ||
828 | if (!dma_len) | |
829 | return 0; | |
830 | ||
831 | if (use_fmr == SRP_MAP_NO_FMR) { | |
832 | /* Once we're in direct map mode for a request, we don't | |
833 | * go back to FMR mode, so no need to update anything | |
834 | * other than the descriptor. | |
835 | */ | |
836 | srp_map_desc(state, dma_addr, dma_len, target->rkey); | |
837 | return 0; | |
85507bcc | 838 | } |
f5358a17 | 839 | |
8f26c9ff DD |
840 | /* If we start at an offset into the FMR page, don't merge into |
841 | * the current FMR. Finish it out, and use the kernel's MR for this | |
842 | * sg entry. This is to avoid potential bugs on some SRP targets | |
843 | * that were never quite defined, but went away when the initiator | |
844 | * avoided using FMR on such page fragments. | |
845 | */ | |
846 | if (dma_addr & ~dev->fmr_page_mask || dma_len > dev->fmr_max_size) { | |
847 | ret = srp_map_finish_fmr(state, target); | |
848 | if (ret) | |
849 | return ret; | |
850 | ||
851 | srp_map_desc(state, dma_addr, dma_len, target->rkey); | |
852 | srp_map_update_start(state, NULL, 0, 0); | |
853 | return 0; | |
f5358a17 RD |
854 | } |
855 | ||
8f26c9ff DD |
856 | /* If this is the first sg to go into the FMR, save our position. |
857 | * We need to know the first unmapped entry, its index, and the | |
858 | * first unmapped address within that entry to be able to restart | |
859 | * mapping after an error. | |
860 | */ | |
861 | if (!state->unmapped_sg) | |
862 | srp_map_update_start(state, sg, sg_index, dma_addr); | |
f5358a17 | 863 | |
8f26c9ff DD |
864 | while (dma_len) { |
865 | if (state->npages == SRP_FMR_SIZE) { | |
866 | ret = srp_map_finish_fmr(state, target); | |
867 | if (ret) | |
868 | return ret; | |
f5358a17 | 869 | |
8f26c9ff DD |
870 | srp_map_update_start(state, sg, sg_index, dma_addr); |
871 | } | |
872 | ||
873 | len = min_t(unsigned int, dma_len, dev->fmr_page_size); | |
f5358a17 | 874 | |
8f26c9ff DD |
875 | if (!state->npages) |
876 | state->base_dma_addr = dma_addr; | |
877 | state->pages[state->npages++] = dma_addr; | |
878 | state->fmr_len += len; | |
879 | dma_addr += len; | |
880 | dma_len -= len; | |
881 | } | |
882 | ||
883 | /* If the last entry of the FMR wasn't a full page, then we need to | |
884 | * close it out and start a new one -- we can only merge at page | |
885 | * boundries. | |
886 | */ | |
887 | ret = 0; | |
888 | if (len != dev->fmr_page_size) { | |
889 | ret = srp_map_finish_fmr(state, target); | |
890 | if (!ret) | |
891 | srp_map_update_start(state, NULL, 0, 0); | |
892 | } | |
f5358a17 RD |
893 | return ret; |
894 | } | |
895 | ||
aef9ec39 RD |
896 | static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target, |
897 | struct srp_request *req) | |
898 | { | |
8f26c9ff | 899 | struct scatterlist *scat, *sg; |
aef9ec39 | 900 | struct srp_cmd *cmd = req->cmd->buf; |
8f26c9ff | 901 | int i, len, nents, count, use_fmr; |
85507bcc RC |
902 | struct srp_device *dev; |
903 | struct ib_device *ibdev; | |
8f26c9ff DD |
904 | struct srp_map_state state; |
905 | struct srp_indirect_buf *indirect_hdr; | |
8f26c9ff DD |
906 | u32 table_len; |
907 | u8 fmt; | |
aef9ec39 | 908 | |
bb350d1d | 909 | if (!scsi_sglist(scmnd) || scmnd->sc_data_direction == DMA_NONE) |
aef9ec39 RD |
910 | return sizeof (struct srp_cmd); |
911 | ||
912 | if (scmnd->sc_data_direction != DMA_FROM_DEVICE && | |
913 | scmnd->sc_data_direction != DMA_TO_DEVICE) { | |
7aa54bd7 DD |
914 | shost_printk(KERN_WARNING, target->scsi_host, |
915 | PFX "Unhandled data direction %d\n", | |
916 | scmnd->sc_data_direction); | |
aef9ec39 RD |
917 | return -EINVAL; |
918 | } | |
919 | ||
bb350d1d FT |
920 | nents = scsi_sg_count(scmnd); |
921 | scat = scsi_sglist(scmnd); | |
aef9ec39 | 922 | |
05321937 | 923 | dev = target->srp_host->srp_dev; |
85507bcc RC |
924 | ibdev = dev->dev; |
925 | ||
926 | count = ib_dma_map_sg(ibdev, scat, nents, scmnd->sc_data_direction); | |
8f26c9ff DD |
927 | if (unlikely(count == 0)) |
928 | return -EIO; | |
f5358a17 RD |
929 | |
930 | fmt = SRP_DATA_DESC_DIRECT; | |
931 | len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf); | |
aef9ec39 | 932 | |
cf368713 | 933 | if (count == 1) { |
f5358a17 RD |
934 | /* |
935 | * The midlayer only generated a single gather/scatter | |
936 | * entry, or DMA mapping coalesced everything to a | |
937 | * single entry. So a direct descriptor along with | |
938 | * the DMA MR suffices. | |
939 | */ | |
cf368713 | 940 | struct srp_direct_buf *buf = (void *) cmd->add_data; |
aef9ec39 | 941 | |
85507bcc | 942 | buf->va = cpu_to_be64(ib_sg_dma_address(ibdev, scat)); |
9af76271 | 943 | buf->key = cpu_to_be32(target->rkey); |
85507bcc | 944 | buf->len = cpu_to_be32(ib_sg_dma_len(ibdev, scat)); |
8f26c9ff DD |
945 | |
946 | req->nfmr = 0; | |
947 | goto map_complete; | |
948 | } | |
949 | ||
950 | /* We have more than one scatter/gather entry, so build our indirect | |
951 | * descriptor table, trying to merge as many entries with FMR as we | |
952 | * can. | |
953 | */ | |
954 | indirect_hdr = (void *) cmd->add_data; | |
955 | ||
c07d424d DD |
956 | ib_dma_sync_single_for_cpu(ibdev, req->indirect_dma_addr, |
957 | target->indirect_size, DMA_TO_DEVICE); | |
958 | ||
8f26c9ff | 959 | memset(&state, 0, sizeof(state)); |
c07d424d | 960 | state.desc = req->indirect_desc; |
8f26c9ff DD |
961 | state.pages = req->map_page; |
962 | state.next_fmr = req->fmr_list; | |
963 | ||
964 | use_fmr = dev->fmr_pool ? SRP_MAP_ALLOW_FMR : SRP_MAP_NO_FMR; | |
965 | ||
966 | for_each_sg(scat, sg, count, i) { | |
967 | if (srp_map_sg_entry(&state, target, sg, i, use_fmr)) { | |
968 | /* FMR mapping failed, so backtrack to the first | |
969 | * unmapped entry and continue on without using FMR. | |
970 | */ | |
971 | dma_addr_t dma_addr; | |
972 | unsigned int dma_len; | |
973 | ||
974 | backtrack: | |
975 | sg = state.unmapped_sg; | |
976 | i = state.unmapped_index; | |
977 | ||
978 | dma_addr = ib_sg_dma_address(ibdev, sg); | |
979 | dma_len = ib_sg_dma_len(ibdev, sg); | |
980 | dma_len -= (state.unmapped_addr - dma_addr); | |
981 | dma_addr = state.unmapped_addr; | |
982 | use_fmr = SRP_MAP_NO_FMR; | |
983 | srp_map_desc(&state, dma_addr, dma_len, target->rkey); | |
f5358a17 | 984 | } |
8f26c9ff | 985 | } |
aef9ec39 | 986 | |
8f26c9ff DD |
987 | if (use_fmr == SRP_MAP_ALLOW_FMR && srp_map_finish_fmr(&state, target)) |
988 | goto backtrack; | |
cf368713 | 989 | |
c07d424d DD |
990 | /* We've mapped the request, now pull as much of the indirect |
991 | * descriptor table as we can into the command buffer. If this | |
992 | * target is not using an external indirect table, we are | |
993 | * guaranteed to fit into the command, as the SCSI layer won't | |
994 | * give us more S/G entries than we allow. | |
8f26c9ff DD |
995 | */ |
996 | req->nfmr = state.nfmr; | |
997 | if (state.ndesc == 1) { | |
998 | /* FMR mapping was able to collapse this to one entry, | |
999 | * so use a direct descriptor. | |
1000 | */ | |
1001 | struct srp_direct_buf *buf = (void *) cmd->add_data; | |
cf368713 | 1002 | |
c07d424d | 1003 | *buf = req->indirect_desc[0]; |
8f26c9ff | 1004 | goto map_complete; |
aef9ec39 RD |
1005 | } |
1006 | ||
c07d424d DD |
1007 | if (unlikely(target->cmd_sg_cnt < state.ndesc && |
1008 | !target->allow_ext_sg)) { | |
1009 | shost_printk(KERN_ERR, target->scsi_host, | |
1010 | "Could not fit S/G list into SRP_CMD\n"); | |
1011 | return -EIO; | |
1012 | } | |
1013 | ||
1014 | count = min(state.ndesc, target->cmd_sg_cnt); | |
8f26c9ff DD |
1015 | table_len = state.ndesc * sizeof (struct srp_direct_buf); |
1016 | ||
1017 | fmt = SRP_DATA_DESC_INDIRECT; | |
1018 | len = sizeof(struct srp_cmd) + sizeof (struct srp_indirect_buf); | |
c07d424d | 1019 | len += count * sizeof (struct srp_direct_buf); |
8f26c9ff | 1020 | |
c07d424d DD |
1021 | memcpy(indirect_hdr->desc_list, req->indirect_desc, |
1022 | count * sizeof (struct srp_direct_buf)); | |
8f26c9ff | 1023 | |
c07d424d | 1024 | indirect_hdr->table_desc.va = cpu_to_be64(req->indirect_dma_addr); |
8f26c9ff DD |
1025 | indirect_hdr->table_desc.key = cpu_to_be32(target->rkey); |
1026 | indirect_hdr->table_desc.len = cpu_to_be32(table_len); | |
1027 | indirect_hdr->len = cpu_to_be32(state.total_len); | |
1028 | ||
1029 | if (scmnd->sc_data_direction == DMA_TO_DEVICE) | |
c07d424d | 1030 | cmd->data_out_desc_cnt = count; |
8f26c9ff | 1031 | else |
c07d424d DD |
1032 | cmd->data_in_desc_cnt = count; |
1033 | ||
1034 | ib_dma_sync_single_for_device(ibdev, req->indirect_dma_addr, table_len, | |
1035 | DMA_TO_DEVICE); | |
8f26c9ff DD |
1036 | |
1037 | map_complete: | |
aef9ec39 RD |
1038 | if (scmnd->sc_data_direction == DMA_TO_DEVICE) |
1039 | cmd->buf_fmt = fmt << 4; | |
1040 | else | |
1041 | cmd->buf_fmt = fmt; | |
1042 | ||
aef9ec39 RD |
1043 | return len; |
1044 | } | |
1045 | ||
76c75b25 BVA |
1046 | /* |
1047 | * Return an IU and possible credit to the free pool | |
1048 | */ | |
1049 | static void srp_put_tx_iu(struct srp_target_port *target, struct srp_iu *iu, | |
1050 | enum srp_iu_type iu_type) | |
1051 | { | |
1052 | unsigned long flags; | |
1053 | ||
e9684678 | 1054 | spin_lock_irqsave(&target->lock, flags); |
76c75b25 BVA |
1055 | list_add(&iu->list, &target->free_tx); |
1056 | if (iu_type != SRP_IU_RSP) | |
1057 | ++target->req_lim; | |
e9684678 | 1058 | spin_unlock_irqrestore(&target->lock, flags); |
76c75b25 BVA |
1059 | } |
1060 | ||
05a1d750 | 1061 | /* |
e9684678 BVA |
1062 | * Must be called with target->lock held to protect req_lim and free_tx. |
1063 | * If IU is not sent, it must be returned using srp_put_tx_iu(). | |
05a1d750 DD |
1064 | * |
1065 | * Note: | |
1066 | * An upper limit for the number of allocated information units for each | |
1067 | * request type is: | |
1068 | * - SRP_IU_CMD: SRP_CMD_SQ_SIZE, since the SCSI mid-layer never queues | |
1069 | * more than Scsi_Host.can_queue requests. | |
1070 | * - SRP_IU_TSK_MGMT: SRP_TSK_MGMT_SQ_SIZE. | |
1071 | * - SRP_IU_RSP: 1, since a conforming SRP target never sends more than | |
1072 | * one unanswered SRP request to an initiator. | |
1073 | */ | |
1074 | static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target, | |
1075 | enum srp_iu_type iu_type) | |
1076 | { | |
1077 | s32 rsv = (iu_type == SRP_IU_TSK_MGMT) ? 0 : SRP_TSK_MGMT_SQ_SIZE; | |
1078 | struct srp_iu *iu; | |
1079 | ||
1080 | srp_send_completion(target->send_cq, target); | |
1081 | ||
dcb4cb85 | 1082 | if (list_empty(&target->free_tx)) |
05a1d750 DD |
1083 | return NULL; |
1084 | ||
1085 | /* Initiator responses to target requests do not consume credits */ | |
76c75b25 BVA |
1086 | if (iu_type != SRP_IU_RSP) { |
1087 | if (target->req_lim <= rsv) { | |
1088 | ++target->zero_req_lim; | |
1089 | return NULL; | |
1090 | } | |
1091 | ||
1092 | --target->req_lim; | |
05a1d750 DD |
1093 | } |
1094 | ||
dcb4cb85 | 1095 | iu = list_first_entry(&target->free_tx, struct srp_iu, list); |
76c75b25 | 1096 | list_del(&iu->list); |
05a1d750 DD |
1097 | return iu; |
1098 | } | |
1099 | ||
76c75b25 BVA |
1100 | static int srp_post_send(struct srp_target_port *target, |
1101 | struct srp_iu *iu, int len) | |
05a1d750 DD |
1102 | { |
1103 | struct ib_sge list; | |
1104 | struct ib_send_wr wr, *bad_wr; | |
05a1d750 DD |
1105 | |
1106 | list.addr = iu->dma; | |
1107 | list.length = len; | |
9af76271 | 1108 | list.lkey = target->lkey; |
05a1d750 DD |
1109 | |
1110 | wr.next = NULL; | |
dcb4cb85 | 1111 | wr.wr_id = (uintptr_t) iu; |
05a1d750 DD |
1112 | wr.sg_list = &list; |
1113 | wr.num_sge = 1; | |
1114 | wr.opcode = IB_WR_SEND; | |
1115 | wr.send_flags = IB_SEND_SIGNALED; | |
1116 | ||
76c75b25 | 1117 | return ib_post_send(target->qp, &wr, &bad_wr); |
05a1d750 DD |
1118 | } |
1119 | ||
dcb4cb85 | 1120 | static int srp_post_recv(struct srp_target_port *target, struct srp_iu *iu) |
c996bb47 | 1121 | { |
c996bb47 | 1122 | struct ib_recv_wr wr, *bad_wr; |
dcb4cb85 | 1123 | struct ib_sge list; |
c996bb47 BVA |
1124 | |
1125 | list.addr = iu->dma; | |
1126 | list.length = iu->size; | |
9af76271 | 1127 | list.lkey = target->lkey; |
c996bb47 BVA |
1128 | |
1129 | wr.next = NULL; | |
dcb4cb85 | 1130 | wr.wr_id = (uintptr_t) iu; |
c996bb47 BVA |
1131 | wr.sg_list = &list; |
1132 | wr.num_sge = 1; | |
1133 | ||
dcb4cb85 | 1134 | return ib_post_recv(target->qp, &wr, &bad_wr); |
c996bb47 BVA |
1135 | } |
1136 | ||
aef9ec39 RD |
1137 | static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp) |
1138 | { | |
1139 | struct srp_request *req; | |
1140 | struct scsi_cmnd *scmnd; | |
1141 | unsigned long flags; | |
aef9ec39 | 1142 | |
aef9ec39 | 1143 | if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) { |
e9684678 | 1144 | spin_lock_irqsave(&target->lock, flags); |
94a9174c | 1145 | target->req_lim += be32_to_cpu(rsp->req_lim_delta); |
e9684678 | 1146 | spin_unlock_irqrestore(&target->lock, flags); |
94a9174c | 1147 | |
f8b6e31e DD |
1148 | target->tsk_mgmt_status = -1; |
1149 | if (be32_to_cpu(rsp->resp_data_len) >= 4) | |
1150 | target->tsk_mgmt_status = rsp->data[3]; | |
1151 | complete(&target->tsk_mgmt_done); | |
aef9ec39 | 1152 | } else { |
f8b6e31e | 1153 | req = &target->req_ring[rsp->tag]; |
22032991 BVA |
1154 | scmnd = srp_claim_req(target, req, NULL); |
1155 | if (!scmnd) { | |
7aa54bd7 DD |
1156 | shost_printk(KERN_ERR, target->scsi_host, |
1157 | "Null scmnd for RSP w/tag %016llx\n", | |
1158 | (unsigned long long) rsp->tag); | |
22032991 BVA |
1159 | |
1160 | spin_lock_irqsave(&target->lock, flags); | |
1161 | target->req_lim += be32_to_cpu(rsp->req_lim_delta); | |
1162 | spin_unlock_irqrestore(&target->lock, flags); | |
1163 | ||
1164 | return; | |
1165 | } | |
aef9ec39 RD |
1166 | scmnd->result = rsp->status; |
1167 | ||
1168 | if (rsp->flags & SRP_RSP_FLAG_SNSVALID) { | |
1169 | memcpy(scmnd->sense_buffer, rsp->data + | |
1170 | be32_to_cpu(rsp->resp_data_len), | |
1171 | min_t(int, be32_to_cpu(rsp->sense_data_len), | |
1172 | SCSI_SENSE_BUFFERSIZE)); | |
1173 | } | |
1174 | ||
1175 | if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER)) | |
bb350d1d | 1176 | scsi_set_resid(scmnd, be32_to_cpu(rsp->data_out_res_cnt)); |
aef9ec39 | 1177 | else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER)) |
bb350d1d | 1178 | scsi_set_resid(scmnd, be32_to_cpu(rsp->data_in_res_cnt)); |
aef9ec39 | 1179 | |
22032991 BVA |
1180 | srp_free_req(target, req, scmnd, |
1181 | be32_to_cpu(rsp->req_lim_delta)); | |
1182 | ||
f8b6e31e DD |
1183 | scmnd->host_scribble = NULL; |
1184 | scmnd->scsi_done(scmnd); | |
aef9ec39 | 1185 | } |
aef9ec39 RD |
1186 | } |
1187 | ||
bb12588a DD |
1188 | static int srp_response_common(struct srp_target_port *target, s32 req_delta, |
1189 | void *rsp, int len) | |
1190 | { | |
76c75b25 | 1191 | struct ib_device *dev = target->srp_host->srp_dev->dev; |
bb12588a DD |
1192 | unsigned long flags; |
1193 | struct srp_iu *iu; | |
76c75b25 | 1194 | int err; |
bb12588a | 1195 | |
e9684678 | 1196 | spin_lock_irqsave(&target->lock, flags); |
bb12588a | 1197 | target->req_lim += req_delta; |
bb12588a | 1198 | iu = __srp_get_tx_iu(target, SRP_IU_RSP); |
e9684678 | 1199 | spin_unlock_irqrestore(&target->lock, flags); |
76c75b25 | 1200 | |
bb12588a DD |
1201 | if (!iu) { |
1202 | shost_printk(KERN_ERR, target->scsi_host, PFX | |
1203 | "no IU available to send response\n"); | |
76c75b25 | 1204 | return 1; |
bb12588a DD |
1205 | } |
1206 | ||
1207 | ib_dma_sync_single_for_cpu(dev, iu->dma, len, DMA_TO_DEVICE); | |
1208 | memcpy(iu->buf, rsp, len); | |
1209 | ib_dma_sync_single_for_device(dev, iu->dma, len, DMA_TO_DEVICE); | |
1210 | ||
76c75b25 BVA |
1211 | err = srp_post_send(target, iu, len); |
1212 | if (err) { | |
bb12588a DD |
1213 | shost_printk(KERN_ERR, target->scsi_host, PFX |
1214 | "unable to post response: %d\n", err); | |
76c75b25 BVA |
1215 | srp_put_tx_iu(target, iu, SRP_IU_RSP); |
1216 | } | |
bb12588a | 1217 | |
bb12588a DD |
1218 | return err; |
1219 | } | |
1220 | ||
1221 | static void srp_process_cred_req(struct srp_target_port *target, | |
1222 | struct srp_cred_req *req) | |
1223 | { | |
1224 | struct srp_cred_rsp rsp = { | |
1225 | .opcode = SRP_CRED_RSP, | |
1226 | .tag = req->tag, | |
1227 | }; | |
1228 | s32 delta = be32_to_cpu(req->req_lim_delta); | |
1229 | ||
1230 | if (srp_response_common(target, delta, &rsp, sizeof rsp)) | |
1231 | shost_printk(KERN_ERR, target->scsi_host, PFX | |
1232 | "problems processing SRP_CRED_REQ\n"); | |
1233 | } | |
1234 | ||
1235 | static void srp_process_aer_req(struct srp_target_port *target, | |
1236 | struct srp_aer_req *req) | |
1237 | { | |
1238 | struct srp_aer_rsp rsp = { | |
1239 | .opcode = SRP_AER_RSP, | |
1240 | .tag = req->tag, | |
1241 | }; | |
1242 | s32 delta = be32_to_cpu(req->req_lim_delta); | |
1243 | ||
1244 | shost_printk(KERN_ERR, target->scsi_host, PFX | |
1245 | "ignoring AER for LUN %llu\n", be64_to_cpu(req->lun)); | |
1246 | ||
1247 | if (srp_response_common(target, delta, &rsp, sizeof rsp)) | |
1248 | shost_printk(KERN_ERR, target->scsi_host, PFX | |
1249 | "problems processing SRP_AER_REQ\n"); | |
1250 | } | |
1251 | ||
aef9ec39 RD |
1252 | static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc) |
1253 | { | |
dcb4cb85 | 1254 | struct ib_device *dev = target->srp_host->srp_dev->dev; |
737b94eb | 1255 | struct srp_iu *iu = (struct srp_iu *) (uintptr_t) wc->wr_id; |
c996bb47 | 1256 | int res; |
aef9ec39 RD |
1257 | u8 opcode; |
1258 | ||
85507bcc RC |
1259 | ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_ti_iu_len, |
1260 | DMA_FROM_DEVICE); | |
aef9ec39 RD |
1261 | |
1262 | opcode = *(u8 *) iu->buf; | |
1263 | ||
1264 | if (0) { | |
7aa54bd7 DD |
1265 | shost_printk(KERN_ERR, target->scsi_host, |
1266 | PFX "recv completion, opcode 0x%02x\n", opcode); | |
7a700811 BVA |
1267 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_OFFSET, 8, 1, |
1268 | iu->buf, wc->byte_len, true); | |
aef9ec39 RD |
1269 | } |
1270 | ||
1271 | switch (opcode) { | |
1272 | case SRP_RSP: | |
1273 | srp_process_rsp(target, iu->buf); | |
1274 | break; | |
1275 | ||
bb12588a DD |
1276 | case SRP_CRED_REQ: |
1277 | srp_process_cred_req(target, iu->buf); | |
1278 | break; | |
1279 | ||
1280 | case SRP_AER_REQ: | |
1281 | srp_process_aer_req(target, iu->buf); | |
1282 | break; | |
1283 | ||
aef9ec39 RD |
1284 | case SRP_T_LOGOUT: |
1285 | /* XXX Handle target logout */ | |
7aa54bd7 DD |
1286 | shost_printk(KERN_WARNING, target->scsi_host, |
1287 | PFX "Got target logout request\n"); | |
aef9ec39 RD |
1288 | break; |
1289 | ||
1290 | default: | |
7aa54bd7 DD |
1291 | shost_printk(KERN_WARNING, target->scsi_host, |
1292 | PFX "Unhandled SRP opcode 0x%02x\n", opcode); | |
aef9ec39 RD |
1293 | break; |
1294 | } | |
1295 | ||
85507bcc RC |
1296 | ib_dma_sync_single_for_device(dev, iu->dma, target->max_ti_iu_len, |
1297 | DMA_FROM_DEVICE); | |
c996bb47 | 1298 | |
dcb4cb85 | 1299 | res = srp_post_recv(target, iu); |
c996bb47 BVA |
1300 | if (res != 0) |
1301 | shost_printk(KERN_ERR, target->scsi_host, | |
1302 | PFX "Recv failed with error code %d\n", res); | |
aef9ec39 RD |
1303 | } |
1304 | ||
948d1e88 BVA |
1305 | static void srp_handle_qp_err(enum ib_wc_status wc_status, |
1306 | enum ib_wc_opcode wc_opcode, | |
1307 | struct srp_target_port *target) | |
1308 | { | |
294c875a | 1309 | if (target->connected && !target->qp_in_error) { |
4f0af697 BVA |
1310 | shost_printk(KERN_ERR, target->scsi_host, |
1311 | PFX "failed %s status %d\n", | |
1312 | wc_opcode & IB_WC_RECV ? "receive" : "send", | |
1313 | wc_status); | |
1314 | } | |
948d1e88 BVA |
1315 | target->qp_in_error = true; |
1316 | } | |
1317 | ||
9c03dc9f | 1318 | static void srp_recv_completion(struct ib_cq *cq, void *target_ptr) |
aef9ec39 RD |
1319 | { |
1320 | struct srp_target_port *target = target_ptr; | |
1321 | struct ib_wc wc; | |
aef9ec39 RD |
1322 | |
1323 | ib_req_notify_cq(cq, IB_CQ_NEXT_COMP); | |
1324 | while (ib_poll_cq(cq, 1, &wc) > 0) { | |
948d1e88 BVA |
1325 | if (likely(wc.status == IB_WC_SUCCESS)) { |
1326 | srp_handle_recv(target, &wc); | |
1327 | } else { | |
1328 | srp_handle_qp_err(wc.status, wc.opcode, target); | |
aef9ec39 | 1329 | } |
9c03dc9f BVA |
1330 | } |
1331 | } | |
1332 | ||
1333 | static void srp_send_completion(struct ib_cq *cq, void *target_ptr) | |
1334 | { | |
1335 | struct srp_target_port *target = target_ptr; | |
1336 | struct ib_wc wc; | |
dcb4cb85 | 1337 | struct srp_iu *iu; |
9c03dc9f BVA |
1338 | |
1339 | while (ib_poll_cq(cq, 1, &wc) > 0) { | |
948d1e88 BVA |
1340 | if (likely(wc.status == IB_WC_SUCCESS)) { |
1341 | iu = (struct srp_iu *) (uintptr_t) wc.wr_id; | |
1342 | list_add(&iu->list, &target->free_tx); | |
1343 | } else { | |
1344 | srp_handle_qp_err(wc.status, wc.opcode, target); | |
9c03dc9f | 1345 | } |
aef9ec39 RD |
1346 | } |
1347 | } | |
1348 | ||
76c75b25 | 1349 | static int srp_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *scmnd) |
aef9ec39 | 1350 | { |
76c75b25 | 1351 | struct srp_target_port *target = host_to_target(shost); |
aef9ec39 RD |
1352 | struct srp_request *req; |
1353 | struct srp_iu *iu; | |
1354 | struct srp_cmd *cmd; | |
85507bcc | 1355 | struct ib_device *dev; |
76c75b25 | 1356 | unsigned long flags; |
aef9ec39 RD |
1357 | int len; |
1358 | ||
2ce19e72 BVA |
1359 | if (unlikely(target->transport_offline)) { |
1360 | scmnd->result = DID_NO_CONNECT << 16; | |
1361 | scmnd->scsi_done(scmnd); | |
1362 | return 0; | |
1363 | } | |
1364 | ||
e9684678 | 1365 | spin_lock_irqsave(&target->lock, flags); |
bb12588a | 1366 | iu = __srp_get_tx_iu(target, SRP_IU_CMD); |
aef9ec39 | 1367 | if (!iu) |
695b8349 BVA |
1368 | goto err_unlock; |
1369 | ||
1370 | req = list_first_entry(&target->free_reqs, struct srp_request, list); | |
1371 | list_del(&req->list); | |
1372 | spin_unlock_irqrestore(&target->lock, flags); | |
aef9ec39 | 1373 | |
05321937 | 1374 | dev = target->srp_host->srp_dev->dev; |
49248644 | 1375 | ib_dma_sync_single_for_cpu(dev, iu->dma, target->max_iu_len, |
85507bcc | 1376 | DMA_TO_DEVICE); |
aef9ec39 | 1377 | |
aef9ec39 | 1378 | scmnd->result = 0; |
f8b6e31e | 1379 | scmnd->host_scribble = (void *) req; |
aef9ec39 RD |
1380 | |
1381 | cmd = iu->buf; | |
1382 | memset(cmd, 0, sizeof *cmd); | |
1383 | ||
1384 | cmd->opcode = SRP_CMD; | |
1385 | cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48); | |
d945e1df | 1386 | cmd->tag = req->index; |
aef9ec39 RD |
1387 | memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len); |
1388 | ||
aef9ec39 RD |
1389 | req->scmnd = scmnd; |
1390 | req->cmd = iu; | |
aef9ec39 RD |
1391 | |
1392 | len = srp_map_data(scmnd, target, req); | |
1393 | if (len < 0) { | |
7aa54bd7 DD |
1394 | shost_printk(KERN_ERR, target->scsi_host, |
1395 | PFX "Failed to map data\n"); | |
76c75b25 | 1396 | goto err_iu; |
aef9ec39 RD |
1397 | } |
1398 | ||
49248644 | 1399 | ib_dma_sync_single_for_device(dev, iu->dma, target->max_iu_len, |
85507bcc | 1400 | DMA_TO_DEVICE); |
aef9ec39 | 1401 | |
76c75b25 | 1402 | if (srp_post_send(target, iu, len)) { |
7aa54bd7 | 1403 | shost_printk(KERN_ERR, target->scsi_host, PFX "Send failed\n"); |
aef9ec39 RD |
1404 | goto err_unmap; |
1405 | } | |
1406 | ||
aef9ec39 RD |
1407 | return 0; |
1408 | ||
1409 | err_unmap: | |
1410 | srp_unmap_data(scmnd, target, req); | |
1411 | ||
76c75b25 BVA |
1412 | err_iu: |
1413 | srp_put_tx_iu(target, iu, SRP_IU_CMD); | |
1414 | ||
e9684678 | 1415 | spin_lock_irqsave(&target->lock, flags); |
76c75b25 | 1416 | list_add(&req->list, &target->free_reqs); |
695b8349 BVA |
1417 | |
1418 | err_unlock: | |
e9684678 | 1419 | spin_unlock_irqrestore(&target->lock, flags); |
76c75b25 | 1420 | |
aef9ec39 RD |
1421 | return SCSI_MLQUEUE_HOST_BUSY; |
1422 | } | |
1423 | ||
1424 | static int srp_alloc_iu_bufs(struct srp_target_port *target) | |
1425 | { | |
1426 | int i; | |
1427 | ||
1428 | for (i = 0; i < SRP_RQ_SIZE; ++i) { | |
1429 | target->rx_ring[i] = srp_alloc_iu(target->srp_host, | |
1430 | target->max_ti_iu_len, | |
1431 | GFP_KERNEL, DMA_FROM_DEVICE); | |
1432 | if (!target->rx_ring[i]) | |
1433 | goto err; | |
1434 | } | |
1435 | ||
dd5e6e38 | 1436 | for (i = 0; i < SRP_SQ_SIZE; ++i) { |
aef9ec39 | 1437 | target->tx_ring[i] = srp_alloc_iu(target->srp_host, |
49248644 | 1438 | target->max_iu_len, |
aef9ec39 RD |
1439 | GFP_KERNEL, DMA_TO_DEVICE); |
1440 | if (!target->tx_ring[i]) | |
1441 | goto err; | |
dcb4cb85 BVA |
1442 | |
1443 | list_add(&target->tx_ring[i]->list, &target->free_tx); | |
aef9ec39 RD |
1444 | } |
1445 | ||
1446 | return 0; | |
1447 | ||
1448 | err: | |
1449 | for (i = 0; i < SRP_RQ_SIZE; ++i) { | |
1450 | srp_free_iu(target->srp_host, target->rx_ring[i]); | |
1451 | target->rx_ring[i] = NULL; | |
1452 | } | |
1453 | ||
dd5e6e38 | 1454 | for (i = 0; i < SRP_SQ_SIZE; ++i) { |
aef9ec39 RD |
1455 | srp_free_iu(target->srp_host, target->tx_ring[i]); |
1456 | target->tx_ring[i] = NULL; | |
1457 | } | |
1458 | ||
1459 | return -ENOMEM; | |
1460 | } | |
1461 | ||
c9b03c1a BVA |
1462 | static uint32_t srp_compute_rq_tmo(struct ib_qp_attr *qp_attr, int attr_mask) |
1463 | { | |
1464 | uint64_t T_tr_ns, max_compl_time_ms; | |
1465 | uint32_t rq_tmo_jiffies; | |
1466 | ||
1467 | /* | |
1468 | * According to section 11.2.4.2 in the IBTA spec (Modify Queue Pair, | |
1469 | * table 91), both the QP timeout and the retry count have to be set | |
1470 | * for RC QP's during the RTR to RTS transition. | |
1471 | */ | |
1472 | WARN_ON_ONCE((attr_mask & (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)) != | |
1473 | (IB_QP_TIMEOUT | IB_QP_RETRY_CNT)); | |
1474 | ||
1475 | /* | |
1476 | * Set target->rq_tmo_jiffies to one second more than the largest time | |
1477 | * it can take before an error completion is generated. See also | |
1478 | * C9-140..142 in the IBTA spec for more information about how to | |
1479 | * convert the QP Local ACK Timeout value to nanoseconds. | |
1480 | */ | |
1481 | T_tr_ns = 4096 * (1ULL << qp_attr->timeout); | |
1482 | max_compl_time_ms = qp_attr->retry_cnt * 4 * T_tr_ns; | |
1483 | do_div(max_compl_time_ms, NSEC_PER_MSEC); | |
1484 | rq_tmo_jiffies = msecs_to_jiffies(max_compl_time_ms + 1000); | |
1485 | ||
1486 | return rq_tmo_jiffies; | |
1487 | } | |
1488 | ||
961e0be8 DD |
1489 | static void srp_cm_rep_handler(struct ib_cm_id *cm_id, |
1490 | struct srp_login_rsp *lrsp, | |
1491 | struct srp_target_port *target) | |
1492 | { | |
1493 | struct ib_qp_attr *qp_attr = NULL; | |
1494 | int attr_mask = 0; | |
1495 | int ret; | |
1496 | int i; | |
1497 | ||
1498 | if (lrsp->opcode == SRP_LOGIN_RSP) { | |
1499 | target->max_ti_iu_len = be32_to_cpu(lrsp->max_ti_iu_len); | |
1500 | target->req_lim = be32_to_cpu(lrsp->req_lim_delta); | |
1501 | ||
1502 | /* | |
1503 | * Reserve credits for task management so we don't | |
1504 | * bounce requests back to the SCSI mid-layer. | |
1505 | */ | |
1506 | target->scsi_host->can_queue | |
1507 | = min(target->req_lim - SRP_TSK_MGMT_SQ_SIZE, | |
1508 | target->scsi_host->can_queue); | |
1509 | } else { | |
1510 | shost_printk(KERN_WARNING, target->scsi_host, | |
1511 | PFX "Unhandled RSP opcode %#x\n", lrsp->opcode); | |
1512 | ret = -ECONNRESET; | |
1513 | goto error; | |
1514 | } | |
1515 | ||
1516 | if (!target->rx_ring[0]) { | |
1517 | ret = srp_alloc_iu_bufs(target); | |
1518 | if (ret) | |
1519 | goto error; | |
1520 | } | |
1521 | ||
1522 | ret = -ENOMEM; | |
1523 | qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL); | |
1524 | if (!qp_attr) | |
1525 | goto error; | |
1526 | ||
1527 | qp_attr->qp_state = IB_QPS_RTR; | |
1528 | ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); | |
1529 | if (ret) | |
1530 | goto error_free; | |
1531 | ||
1532 | ret = ib_modify_qp(target->qp, qp_attr, attr_mask); | |
1533 | if (ret) | |
1534 | goto error_free; | |
1535 | ||
1536 | for (i = 0; i < SRP_RQ_SIZE; i++) { | |
1537 | struct srp_iu *iu = target->rx_ring[i]; | |
1538 | ret = srp_post_recv(target, iu); | |
1539 | if (ret) | |
1540 | goto error_free; | |
1541 | } | |
1542 | ||
1543 | qp_attr->qp_state = IB_QPS_RTS; | |
1544 | ret = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask); | |
1545 | if (ret) | |
1546 | goto error_free; | |
1547 | ||
c9b03c1a BVA |
1548 | target->rq_tmo_jiffies = srp_compute_rq_tmo(qp_attr, attr_mask); |
1549 | ||
961e0be8 DD |
1550 | ret = ib_modify_qp(target->qp, qp_attr, attr_mask); |
1551 | if (ret) | |
1552 | goto error_free; | |
1553 | ||
1554 | ret = ib_send_cm_rtu(cm_id, NULL, 0); | |
1555 | ||
1556 | error_free: | |
1557 | kfree(qp_attr); | |
1558 | ||
1559 | error: | |
1560 | target->status = ret; | |
1561 | } | |
1562 | ||
aef9ec39 RD |
1563 | static void srp_cm_rej_handler(struct ib_cm_id *cm_id, |
1564 | struct ib_cm_event *event, | |
1565 | struct srp_target_port *target) | |
1566 | { | |
7aa54bd7 | 1567 | struct Scsi_Host *shost = target->scsi_host; |
aef9ec39 RD |
1568 | struct ib_class_port_info *cpi; |
1569 | int opcode; | |
1570 | ||
1571 | switch (event->param.rej_rcvd.reason) { | |
1572 | case IB_CM_REJ_PORT_CM_REDIRECT: | |
1573 | cpi = event->param.rej_rcvd.ari; | |
1574 | target->path.dlid = cpi->redirect_lid; | |
1575 | target->path.pkey = cpi->redirect_pkey; | |
1576 | cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff; | |
1577 | memcpy(target->path.dgid.raw, cpi->redirect_gid, 16); | |
1578 | ||
1579 | target->status = target->path.dlid ? | |
1580 | SRP_DLID_REDIRECT : SRP_PORT_REDIRECT; | |
1581 | break; | |
1582 | ||
1583 | case IB_CM_REJ_PORT_REDIRECT: | |
5d7cbfd6 | 1584 | if (srp_target_is_topspin(target)) { |
aef9ec39 RD |
1585 | /* |
1586 | * Topspin/Cisco SRP gateways incorrectly send | |
1587 | * reject reason code 25 when they mean 24 | |
1588 | * (port redirect). | |
1589 | */ | |
1590 | memcpy(target->path.dgid.raw, | |
1591 | event->param.rej_rcvd.ari, 16); | |
1592 | ||
7aa54bd7 DD |
1593 | shost_printk(KERN_DEBUG, shost, |
1594 | PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n", | |
1595 | (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix), | |
1596 | (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id)); | |
aef9ec39 RD |
1597 | |
1598 | target->status = SRP_PORT_REDIRECT; | |
1599 | } else { | |
7aa54bd7 DD |
1600 | shost_printk(KERN_WARNING, shost, |
1601 | " REJ reason: IB_CM_REJ_PORT_REDIRECT\n"); | |
aef9ec39 RD |
1602 | target->status = -ECONNRESET; |
1603 | } | |
1604 | break; | |
1605 | ||
1606 | case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID: | |
7aa54bd7 DD |
1607 | shost_printk(KERN_WARNING, shost, |
1608 | " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n"); | |
aef9ec39 RD |
1609 | target->status = -ECONNRESET; |
1610 | break; | |
1611 | ||
1612 | case IB_CM_REJ_CONSUMER_DEFINED: | |
1613 | opcode = *(u8 *) event->private_data; | |
1614 | if (opcode == SRP_LOGIN_REJ) { | |
1615 | struct srp_login_rej *rej = event->private_data; | |
1616 | u32 reason = be32_to_cpu(rej->reason); | |
1617 | ||
1618 | if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE) | |
7aa54bd7 DD |
1619 | shost_printk(KERN_WARNING, shost, |
1620 | PFX "SRP_LOGIN_REJ: requested max_it_iu_len too large\n"); | |
aef9ec39 | 1621 | else |
7aa54bd7 DD |
1622 | shost_printk(KERN_WARNING, shost, |
1623 | PFX "SRP LOGIN REJECTED, reason 0x%08x\n", reason); | |
aef9ec39 | 1624 | } else |
7aa54bd7 DD |
1625 | shost_printk(KERN_WARNING, shost, |
1626 | " REJ reason: IB_CM_REJ_CONSUMER_DEFINED," | |
1627 | " opcode 0x%02x\n", opcode); | |
aef9ec39 RD |
1628 | target->status = -ECONNRESET; |
1629 | break; | |
1630 | ||
9fe4bcf4 DD |
1631 | case IB_CM_REJ_STALE_CONN: |
1632 | shost_printk(KERN_WARNING, shost, " REJ reason: stale connection\n"); | |
1633 | target->status = SRP_STALE_CONN; | |
1634 | break; | |
1635 | ||
aef9ec39 | 1636 | default: |
7aa54bd7 DD |
1637 | shost_printk(KERN_WARNING, shost, " REJ reason 0x%x\n", |
1638 | event->param.rej_rcvd.reason); | |
aef9ec39 RD |
1639 | target->status = -ECONNRESET; |
1640 | } | |
1641 | } | |
1642 | ||
1643 | static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event) | |
1644 | { | |
1645 | struct srp_target_port *target = cm_id->context; | |
aef9ec39 | 1646 | int comp = 0; |
aef9ec39 RD |
1647 | |
1648 | switch (event->event) { | |
1649 | case IB_CM_REQ_ERROR: | |
7aa54bd7 DD |
1650 | shost_printk(KERN_DEBUG, target->scsi_host, |
1651 | PFX "Sending CM REQ failed\n"); | |
aef9ec39 RD |
1652 | comp = 1; |
1653 | target->status = -ECONNRESET; | |
1654 | break; | |
1655 | ||
1656 | case IB_CM_REP_RECEIVED: | |
1657 | comp = 1; | |
961e0be8 | 1658 | srp_cm_rep_handler(cm_id, event->private_data, target); |
aef9ec39 RD |
1659 | break; |
1660 | ||
1661 | case IB_CM_REJ_RECEIVED: | |
7aa54bd7 | 1662 | shost_printk(KERN_DEBUG, target->scsi_host, PFX "REJ received\n"); |
aef9ec39 RD |
1663 | comp = 1; |
1664 | ||
1665 | srp_cm_rej_handler(cm_id, event, target); | |
1666 | break; | |
1667 | ||
b7ac4ab4 | 1668 | case IB_CM_DREQ_RECEIVED: |
7aa54bd7 DD |
1669 | shost_printk(KERN_WARNING, target->scsi_host, |
1670 | PFX "DREQ received - connection closed\n"); | |
294c875a | 1671 | srp_change_conn_state(target, false); |
b7ac4ab4 | 1672 | if (ib_send_cm_drep(cm_id, NULL, 0)) |
7aa54bd7 DD |
1673 | shost_printk(KERN_ERR, target->scsi_host, |
1674 | PFX "Sending CM DREP failed\n"); | |
aef9ec39 RD |
1675 | break; |
1676 | ||
1677 | case IB_CM_TIMEWAIT_EXIT: | |
7aa54bd7 DD |
1678 | shost_printk(KERN_ERR, target->scsi_host, |
1679 | PFX "connection closed\n"); | |
aef9ec39 | 1680 | |
aef9ec39 RD |
1681 | target->status = 0; |
1682 | break; | |
1683 | ||
b7ac4ab4 IR |
1684 | case IB_CM_MRA_RECEIVED: |
1685 | case IB_CM_DREQ_ERROR: | |
1686 | case IB_CM_DREP_RECEIVED: | |
1687 | break; | |
1688 | ||
aef9ec39 | 1689 | default: |
7aa54bd7 DD |
1690 | shost_printk(KERN_WARNING, target->scsi_host, |
1691 | PFX "Unhandled CM event %d\n", event->event); | |
aef9ec39 RD |
1692 | break; |
1693 | } | |
1694 | ||
1695 | if (comp) | |
1696 | complete(&target->done); | |
1697 | ||
aef9ec39 RD |
1698 | return 0; |
1699 | } | |
1700 | ||
d945e1df | 1701 | static int srp_send_tsk_mgmt(struct srp_target_port *target, |
f8b6e31e | 1702 | u64 req_tag, unsigned int lun, u8 func) |
aef9ec39 | 1703 | { |
19081f31 | 1704 | struct ib_device *dev = target->srp_host->srp_dev->dev; |
aef9ec39 RD |
1705 | struct srp_iu *iu; |
1706 | struct srp_tsk_mgmt *tsk_mgmt; | |
aef9ec39 | 1707 | |
3780d1f0 BVA |
1708 | if (!target->connected || target->qp_in_error) |
1709 | return -1; | |
1710 | ||
f8b6e31e | 1711 | init_completion(&target->tsk_mgmt_done); |
aef9ec39 | 1712 | |
e9684678 | 1713 | spin_lock_irq(&target->lock); |
bb12588a | 1714 | iu = __srp_get_tx_iu(target, SRP_IU_TSK_MGMT); |
e9684678 | 1715 | spin_unlock_irq(&target->lock); |
76c75b25 | 1716 | |
aef9ec39 | 1717 | if (!iu) |
76c75b25 | 1718 | return -1; |
aef9ec39 | 1719 | |
19081f31 DD |
1720 | ib_dma_sync_single_for_cpu(dev, iu->dma, sizeof *tsk_mgmt, |
1721 | DMA_TO_DEVICE); | |
aef9ec39 RD |
1722 | tsk_mgmt = iu->buf; |
1723 | memset(tsk_mgmt, 0, sizeof *tsk_mgmt); | |
1724 | ||
1725 | tsk_mgmt->opcode = SRP_TSK_MGMT; | |
f8b6e31e DD |
1726 | tsk_mgmt->lun = cpu_to_be64((u64) lun << 48); |
1727 | tsk_mgmt->tag = req_tag | SRP_TAG_TSK_MGMT; | |
aef9ec39 | 1728 | tsk_mgmt->tsk_mgmt_func = func; |
f8b6e31e | 1729 | tsk_mgmt->task_tag = req_tag; |
aef9ec39 | 1730 | |
19081f31 DD |
1731 | ib_dma_sync_single_for_device(dev, iu->dma, sizeof *tsk_mgmt, |
1732 | DMA_TO_DEVICE); | |
76c75b25 BVA |
1733 | if (srp_post_send(target, iu, sizeof *tsk_mgmt)) { |
1734 | srp_put_tx_iu(target, iu, SRP_IU_TSK_MGMT); | |
1735 | return -1; | |
1736 | } | |
d945e1df | 1737 | |
f8b6e31e | 1738 | if (!wait_for_completion_timeout(&target->tsk_mgmt_done, |
aef9ec39 | 1739 | msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS))) |
d945e1df | 1740 | return -1; |
aef9ec39 | 1741 | |
d945e1df | 1742 | return 0; |
d945e1df RD |
1743 | } |
1744 | ||
aef9ec39 RD |
1745 | static int srp_abort(struct scsi_cmnd *scmnd) |
1746 | { | |
d945e1df | 1747 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
f8b6e31e | 1748 | struct srp_request *req = (struct srp_request *) scmnd->host_scribble; |
086f44f5 | 1749 | int ret; |
d945e1df | 1750 | |
7aa54bd7 | 1751 | shost_printk(KERN_ERR, target->scsi_host, "SRP abort called\n"); |
aef9ec39 | 1752 | |
c7c4e7ff | 1753 | if (!req || !srp_claim_req(target, req, scmnd)) |
d945e1df | 1754 | return FAILED; |
086f44f5 | 1755 | if (srp_send_tsk_mgmt(target, req->index, scmnd->device->lun, |
80d5e8a2 | 1756 | SRP_TSK_ABORT_TASK) == 0) |
086f44f5 | 1757 | ret = SUCCESS; |
99e1c139 BVA |
1758 | else if (target->transport_offline) |
1759 | ret = FAST_IO_FAIL; | |
086f44f5 BVA |
1760 | else |
1761 | ret = FAILED; | |
22032991 BVA |
1762 | srp_free_req(target, req, scmnd, 0); |
1763 | scmnd->result = DID_ABORT << 16; | |
d8536670 | 1764 | scmnd->scsi_done(scmnd); |
d945e1df | 1765 | |
086f44f5 | 1766 | return ret; |
aef9ec39 RD |
1767 | } |
1768 | ||
1769 | static int srp_reset_device(struct scsi_cmnd *scmnd) | |
1770 | { | |
d945e1df | 1771 | struct srp_target_port *target = host_to_target(scmnd->device->host); |
536ae14e | 1772 | int i; |
d945e1df | 1773 | |
7aa54bd7 | 1774 | shost_printk(KERN_ERR, target->scsi_host, "SRP reset_device called\n"); |
aef9ec39 | 1775 | |
f8b6e31e DD |
1776 | if (srp_send_tsk_mgmt(target, SRP_TAG_NO_REQ, scmnd->device->lun, |
1777 | SRP_TSK_LUN_RESET)) | |
d945e1df | 1778 | return FAILED; |
f8b6e31e | 1779 | if (target->tsk_mgmt_status) |
d945e1df RD |
1780 | return FAILED; |
1781 | ||
536ae14e BVA |
1782 | for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { |
1783 | struct srp_request *req = &target->req_ring[i]; | |
f8b6e31e | 1784 | if (req->scmnd && req->scmnd->device == scmnd->device) |
526b4caa | 1785 | srp_reset_req(target, req); |
536ae14e | 1786 | } |
d945e1df | 1787 | |
d945e1df | 1788 | return SUCCESS; |
aef9ec39 RD |
1789 | } |
1790 | ||
1791 | static int srp_reset_host(struct scsi_cmnd *scmnd) | |
1792 | { | |
1793 | struct srp_target_port *target = host_to_target(scmnd->device->host); | |
1794 | int ret = FAILED; | |
1795 | ||
7aa54bd7 | 1796 | shost_printk(KERN_ERR, target->scsi_host, PFX "SRP reset_host called\n"); |
aef9ec39 RD |
1797 | |
1798 | if (!srp_reconnect_target(target)) | |
1799 | ret = SUCCESS; | |
1800 | ||
1801 | return ret; | |
1802 | } | |
1803 | ||
c9b03c1a BVA |
1804 | static int srp_slave_configure(struct scsi_device *sdev) |
1805 | { | |
1806 | struct Scsi_Host *shost = sdev->host; | |
1807 | struct srp_target_port *target = host_to_target(shost); | |
1808 | struct request_queue *q = sdev->request_queue; | |
1809 | unsigned long timeout; | |
1810 | ||
1811 | if (sdev->type == TYPE_DISK) { | |
1812 | timeout = max_t(unsigned, 30 * HZ, target->rq_tmo_jiffies); | |
1813 | blk_queue_rq_timeout(q, timeout); | |
1814 | } | |
1815 | ||
1816 | return 0; | |
1817 | } | |
1818 | ||
ee959b00 TJ |
1819 | static ssize_t show_id_ext(struct device *dev, struct device_attribute *attr, |
1820 | char *buf) | |
6ecb0c84 | 1821 | { |
ee959b00 | 1822 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
6ecb0c84 | 1823 | |
6ecb0c84 RD |
1824 | return sprintf(buf, "0x%016llx\n", |
1825 | (unsigned long long) be64_to_cpu(target->id_ext)); | |
1826 | } | |
1827 | ||
ee959b00 TJ |
1828 | static ssize_t show_ioc_guid(struct device *dev, struct device_attribute *attr, |
1829 | char *buf) | |
6ecb0c84 | 1830 | { |
ee959b00 | 1831 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
6ecb0c84 | 1832 | |
6ecb0c84 RD |
1833 | return sprintf(buf, "0x%016llx\n", |
1834 | (unsigned long long) be64_to_cpu(target->ioc_guid)); | |
1835 | } | |
1836 | ||
ee959b00 TJ |
1837 | static ssize_t show_service_id(struct device *dev, |
1838 | struct device_attribute *attr, char *buf) | |
6ecb0c84 | 1839 | { |
ee959b00 | 1840 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
6ecb0c84 | 1841 | |
6ecb0c84 RD |
1842 | return sprintf(buf, "0x%016llx\n", |
1843 | (unsigned long long) be64_to_cpu(target->service_id)); | |
1844 | } | |
1845 | ||
ee959b00 TJ |
1846 | static ssize_t show_pkey(struct device *dev, struct device_attribute *attr, |
1847 | char *buf) | |
6ecb0c84 | 1848 | { |
ee959b00 | 1849 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
6ecb0c84 | 1850 | |
6ecb0c84 RD |
1851 | return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey)); |
1852 | } | |
1853 | ||
ee959b00 TJ |
1854 | static ssize_t show_dgid(struct device *dev, struct device_attribute *attr, |
1855 | char *buf) | |
6ecb0c84 | 1856 | { |
ee959b00 | 1857 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
6ecb0c84 | 1858 | |
5b095d98 | 1859 | return sprintf(buf, "%pI6\n", target->path.dgid.raw); |
6ecb0c84 RD |
1860 | } |
1861 | ||
ee959b00 TJ |
1862 | static ssize_t show_orig_dgid(struct device *dev, |
1863 | struct device_attribute *attr, char *buf) | |
3633b3d0 | 1864 | { |
ee959b00 | 1865 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
3633b3d0 | 1866 | |
5b095d98 | 1867 | return sprintf(buf, "%pI6\n", target->orig_dgid); |
3633b3d0 IR |
1868 | } |
1869 | ||
89de7486 BVA |
1870 | static ssize_t show_req_lim(struct device *dev, |
1871 | struct device_attribute *attr, char *buf) | |
1872 | { | |
1873 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); | |
1874 | ||
89de7486 BVA |
1875 | return sprintf(buf, "%d\n", target->req_lim); |
1876 | } | |
1877 | ||
ee959b00 TJ |
1878 | static ssize_t show_zero_req_lim(struct device *dev, |
1879 | struct device_attribute *attr, char *buf) | |
6bfa24fa | 1880 | { |
ee959b00 | 1881 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
6bfa24fa | 1882 | |
6bfa24fa RD |
1883 | return sprintf(buf, "%d\n", target->zero_req_lim); |
1884 | } | |
1885 | ||
ee959b00 TJ |
1886 | static ssize_t show_local_ib_port(struct device *dev, |
1887 | struct device_attribute *attr, char *buf) | |
ded7f1a1 | 1888 | { |
ee959b00 | 1889 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
ded7f1a1 IR |
1890 | |
1891 | return sprintf(buf, "%d\n", target->srp_host->port); | |
1892 | } | |
1893 | ||
ee959b00 TJ |
1894 | static ssize_t show_local_ib_device(struct device *dev, |
1895 | struct device_attribute *attr, char *buf) | |
ded7f1a1 | 1896 | { |
ee959b00 | 1897 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); |
ded7f1a1 | 1898 | |
05321937 | 1899 | return sprintf(buf, "%s\n", target->srp_host->srp_dev->dev->name); |
ded7f1a1 IR |
1900 | } |
1901 | ||
4b5e5f41 BVA |
1902 | static ssize_t show_comp_vector(struct device *dev, |
1903 | struct device_attribute *attr, char *buf) | |
1904 | { | |
1905 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); | |
1906 | ||
1907 | return sprintf(buf, "%d\n", target->comp_vector); | |
1908 | } | |
1909 | ||
49248644 DD |
1910 | static ssize_t show_cmd_sg_entries(struct device *dev, |
1911 | struct device_attribute *attr, char *buf) | |
1912 | { | |
1913 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); | |
1914 | ||
1915 | return sprintf(buf, "%u\n", target->cmd_sg_cnt); | |
1916 | } | |
1917 | ||
c07d424d DD |
1918 | static ssize_t show_allow_ext_sg(struct device *dev, |
1919 | struct device_attribute *attr, char *buf) | |
1920 | { | |
1921 | struct srp_target_port *target = host_to_target(class_to_shost(dev)); | |
1922 | ||
1923 | return sprintf(buf, "%s\n", target->allow_ext_sg ? "true" : "false"); | |
1924 | } | |
1925 | ||
ee959b00 TJ |
1926 | static DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL); |
1927 | static DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL); | |
1928 | static DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL); | |
1929 | static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL); | |
1930 | static DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL); | |
1931 | static DEVICE_ATTR(orig_dgid, S_IRUGO, show_orig_dgid, NULL); | |
89de7486 | 1932 | static DEVICE_ATTR(req_lim, S_IRUGO, show_req_lim, NULL); |
ee959b00 TJ |
1933 | static DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL); |
1934 | static DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL); | |
1935 | static DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL); | |
4b5e5f41 | 1936 | static DEVICE_ATTR(comp_vector, S_IRUGO, show_comp_vector, NULL); |
49248644 | 1937 | static DEVICE_ATTR(cmd_sg_entries, S_IRUGO, show_cmd_sg_entries, NULL); |
c07d424d | 1938 | static DEVICE_ATTR(allow_ext_sg, S_IRUGO, show_allow_ext_sg, NULL); |
ee959b00 TJ |
1939 | |
1940 | static struct device_attribute *srp_host_attrs[] = { | |
1941 | &dev_attr_id_ext, | |
1942 | &dev_attr_ioc_guid, | |
1943 | &dev_attr_service_id, | |
1944 | &dev_attr_pkey, | |
1945 | &dev_attr_dgid, | |
1946 | &dev_attr_orig_dgid, | |
89de7486 | 1947 | &dev_attr_req_lim, |
ee959b00 TJ |
1948 | &dev_attr_zero_req_lim, |
1949 | &dev_attr_local_ib_port, | |
1950 | &dev_attr_local_ib_device, | |
4b5e5f41 | 1951 | &dev_attr_comp_vector, |
49248644 | 1952 | &dev_attr_cmd_sg_entries, |
c07d424d | 1953 | &dev_attr_allow_ext_sg, |
6ecb0c84 RD |
1954 | NULL |
1955 | }; | |
1956 | ||
aef9ec39 RD |
1957 | static struct scsi_host_template srp_template = { |
1958 | .module = THIS_MODULE, | |
b7f008fd RD |
1959 | .name = "InfiniBand SRP initiator", |
1960 | .proc_name = DRV_NAME, | |
c9b03c1a | 1961 | .slave_configure = srp_slave_configure, |
aef9ec39 RD |
1962 | .info = srp_target_info, |
1963 | .queuecommand = srp_queuecommand, | |
1964 | .eh_abort_handler = srp_abort, | |
1965 | .eh_device_reset_handler = srp_reset_device, | |
1966 | .eh_host_reset_handler = srp_reset_host, | |
2742c1da | 1967 | .skip_settle_delay = true, |
49248644 | 1968 | .sg_tablesize = SRP_DEF_SG_TABLESIZE, |
dd5e6e38 | 1969 | .can_queue = SRP_CMD_SQ_SIZE, |
aef9ec39 | 1970 | .this_id = -1, |
dd5e6e38 | 1971 | .cmd_per_lun = SRP_CMD_SQ_SIZE, |
6ecb0c84 RD |
1972 | .use_clustering = ENABLE_CLUSTERING, |
1973 | .shost_attrs = srp_host_attrs | |
aef9ec39 RD |
1974 | }; |
1975 | ||
1976 | static int srp_add_target(struct srp_host *host, struct srp_target_port *target) | |
1977 | { | |
3236822b FT |
1978 | struct srp_rport_identifiers ids; |
1979 | struct srp_rport *rport; | |
1980 | ||
aef9ec39 RD |
1981 | sprintf(target->target_name, "SRP.T10:%016llX", |
1982 | (unsigned long long) be64_to_cpu(target->id_ext)); | |
1983 | ||
05321937 | 1984 | if (scsi_add_host(target->scsi_host, host->srp_dev->dev->dma_device)) |
aef9ec39 RD |
1985 | return -ENODEV; |
1986 | ||
3236822b FT |
1987 | memcpy(ids.port_id, &target->id_ext, 8); |
1988 | memcpy(ids.port_id + 8, &target->ioc_guid, 8); | |
aebd5e47 | 1989 | ids.roles = SRP_RPORT_ROLE_TARGET; |
3236822b FT |
1990 | rport = srp_rport_add(target->scsi_host, &ids); |
1991 | if (IS_ERR(rport)) { | |
1992 | scsi_remove_host(target->scsi_host); | |
1993 | return PTR_ERR(rport); | |
1994 | } | |
1995 | ||
dc1bdbd9 BVA |
1996 | rport->lld_data = target; |
1997 | ||
b3589fd4 | 1998 | spin_lock(&host->target_lock); |
aef9ec39 | 1999 | list_add_tail(&target->list, &host->target_list); |
b3589fd4 | 2000 | spin_unlock(&host->target_lock); |
aef9ec39 RD |
2001 | |
2002 | target->state = SRP_TARGET_LIVE; | |
2003 | ||
aef9ec39 | 2004 | scsi_scan_target(&target->scsi_host->shost_gendev, |
1962a4a1 | 2005 | 0, target->scsi_id, SCAN_WILD_CARD, 0); |
aef9ec39 RD |
2006 | |
2007 | return 0; | |
2008 | } | |
2009 | ||
ee959b00 | 2010 | static void srp_release_dev(struct device *dev) |
aef9ec39 RD |
2011 | { |
2012 | struct srp_host *host = | |
ee959b00 | 2013 | container_of(dev, struct srp_host, dev); |
aef9ec39 RD |
2014 | |
2015 | complete(&host->released); | |
2016 | } | |
2017 | ||
2018 | static struct class srp_class = { | |
2019 | .name = "infiniband_srp", | |
ee959b00 | 2020 | .dev_release = srp_release_dev |
aef9ec39 RD |
2021 | }; |
2022 | ||
96fc248a BVA |
2023 | /** |
2024 | * srp_conn_unique() - check whether the connection to a target is unique | |
2025 | */ | |
2026 | static bool srp_conn_unique(struct srp_host *host, | |
2027 | struct srp_target_port *target) | |
2028 | { | |
2029 | struct srp_target_port *t; | |
2030 | bool ret = false; | |
2031 | ||
2032 | if (target->state == SRP_TARGET_REMOVED) | |
2033 | goto out; | |
2034 | ||
2035 | ret = true; | |
2036 | ||
2037 | spin_lock(&host->target_lock); | |
2038 | list_for_each_entry(t, &host->target_list, list) { | |
2039 | if (t != target && | |
2040 | target->id_ext == t->id_ext && | |
2041 | target->ioc_guid == t->ioc_guid && | |
2042 | target->initiator_ext == t->initiator_ext) { | |
2043 | ret = false; | |
2044 | break; | |
2045 | } | |
2046 | } | |
2047 | spin_unlock(&host->target_lock); | |
2048 | ||
2049 | out: | |
2050 | return ret; | |
2051 | } | |
2052 | ||
aef9ec39 RD |
2053 | /* |
2054 | * Target ports are added by writing | |
2055 | * | |
2056 | * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>, | |
2057 | * pkey=<P_Key>,service_id=<service ID> | |
2058 | * | |
2059 | * to the add_target sysfs attribute. | |
2060 | */ | |
2061 | enum { | |
2062 | SRP_OPT_ERR = 0, | |
2063 | SRP_OPT_ID_EXT = 1 << 0, | |
2064 | SRP_OPT_IOC_GUID = 1 << 1, | |
2065 | SRP_OPT_DGID = 1 << 2, | |
2066 | SRP_OPT_PKEY = 1 << 3, | |
2067 | SRP_OPT_SERVICE_ID = 1 << 4, | |
2068 | SRP_OPT_MAX_SECT = 1 << 5, | |
52fb2b50 | 2069 | SRP_OPT_MAX_CMD_PER_LUN = 1 << 6, |
0c0450db | 2070 | SRP_OPT_IO_CLASS = 1 << 7, |
01cb9bcb | 2071 | SRP_OPT_INITIATOR_EXT = 1 << 8, |
49248644 | 2072 | SRP_OPT_CMD_SG_ENTRIES = 1 << 9, |
c07d424d DD |
2073 | SRP_OPT_ALLOW_EXT_SG = 1 << 10, |
2074 | SRP_OPT_SG_TABLESIZE = 1 << 11, | |
4b5e5f41 | 2075 | SRP_OPT_COMP_VECTOR = 1 << 12, |
aef9ec39 RD |
2076 | SRP_OPT_ALL = (SRP_OPT_ID_EXT | |
2077 | SRP_OPT_IOC_GUID | | |
2078 | SRP_OPT_DGID | | |
2079 | SRP_OPT_PKEY | | |
2080 | SRP_OPT_SERVICE_ID), | |
2081 | }; | |
2082 | ||
a447c093 | 2083 | static const match_table_t srp_opt_tokens = { |
52fb2b50 VP |
2084 | { SRP_OPT_ID_EXT, "id_ext=%s" }, |
2085 | { SRP_OPT_IOC_GUID, "ioc_guid=%s" }, | |
2086 | { SRP_OPT_DGID, "dgid=%s" }, | |
2087 | { SRP_OPT_PKEY, "pkey=%x" }, | |
2088 | { SRP_OPT_SERVICE_ID, "service_id=%s" }, | |
2089 | { SRP_OPT_MAX_SECT, "max_sect=%d" }, | |
2090 | { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" }, | |
0c0450db | 2091 | { SRP_OPT_IO_CLASS, "io_class=%x" }, |
01cb9bcb | 2092 | { SRP_OPT_INITIATOR_EXT, "initiator_ext=%s" }, |
49248644 | 2093 | { SRP_OPT_CMD_SG_ENTRIES, "cmd_sg_entries=%u" }, |
c07d424d DD |
2094 | { SRP_OPT_ALLOW_EXT_SG, "allow_ext_sg=%u" }, |
2095 | { SRP_OPT_SG_TABLESIZE, "sg_tablesize=%u" }, | |
4b5e5f41 | 2096 | { SRP_OPT_COMP_VECTOR, "comp_vector=%u" }, |
52fb2b50 | 2097 | { SRP_OPT_ERR, NULL } |
aef9ec39 RD |
2098 | }; |
2099 | ||
2100 | static int srp_parse_options(const char *buf, struct srp_target_port *target) | |
2101 | { | |
2102 | char *options, *sep_opt; | |
2103 | char *p; | |
2104 | char dgid[3]; | |
2105 | substring_t args[MAX_OPT_ARGS]; | |
2106 | int opt_mask = 0; | |
2107 | int token; | |
2108 | int ret = -EINVAL; | |
2109 | int i; | |
2110 | ||
2111 | options = kstrdup(buf, GFP_KERNEL); | |
2112 | if (!options) | |
2113 | return -ENOMEM; | |
2114 | ||
2115 | sep_opt = options; | |
2116 | while ((p = strsep(&sep_opt, ",")) != NULL) { | |
2117 | if (!*p) | |
2118 | continue; | |
2119 | ||
2120 | token = match_token(p, srp_opt_tokens, args); | |
2121 | opt_mask |= token; | |
2122 | ||
2123 | switch (token) { | |
2124 | case SRP_OPT_ID_EXT: | |
2125 | p = match_strdup(args); | |
a20f3a6d IR |
2126 | if (!p) { |
2127 | ret = -ENOMEM; | |
2128 | goto out; | |
2129 | } | |
aef9ec39 RD |
2130 | target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
2131 | kfree(p); | |
2132 | break; | |
2133 | ||
2134 | case SRP_OPT_IOC_GUID: | |
2135 | p = match_strdup(args); | |
a20f3a6d IR |
2136 | if (!p) { |
2137 | ret = -ENOMEM; | |
2138 | goto out; | |
2139 | } | |
aef9ec39 RD |
2140 | target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
2141 | kfree(p); | |
2142 | break; | |
2143 | ||
2144 | case SRP_OPT_DGID: | |
2145 | p = match_strdup(args); | |
a20f3a6d IR |
2146 | if (!p) { |
2147 | ret = -ENOMEM; | |
2148 | goto out; | |
2149 | } | |
aef9ec39 | 2150 | if (strlen(p) != 32) { |
e0bda7d8 | 2151 | pr_warn("bad dest GID parameter '%s'\n", p); |
ce1823f0 | 2152 | kfree(p); |
aef9ec39 RD |
2153 | goto out; |
2154 | } | |
2155 | ||
2156 | for (i = 0; i < 16; ++i) { | |
2157 | strlcpy(dgid, p + i * 2, 3); | |
2158 | target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16); | |
2159 | } | |
bf17c1c7 | 2160 | kfree(p); |
3633b3d0 | 2161 | memcpy(target->orig_dgid, target->path.dgid.raw, 16); |
aef9ec39 RD |
2162 | break; |
2163 | ||
2164 | case SRP_OPT_PKEY: | |
2165 | if (match_hex(args, &token)) { | |
e0bda7d8 | 2166 | pr_warn("bad P_Key parameter '%s'\n", p); |
aef9ec39 RD |
2167 | goto out; |
2168 | } | |
2169 | target->path.pkey = cpu_to_be16(token); | |
2170 | break; | |
2171 | ||
2172 | case SRP_OPT_SERVICE_ID: | |
2173 | p = match_strdup(args); | |
a20f3a6d IR |
2174 | if (!p) { |
2175 | ret = -ENOMEM; | |
2176 | goto out; | |
2177 | } | |
aef9ec39 | 2178 | target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
247e020e | 2179 | target->path.service_id = target->service_id; |
aef9ec39 RD |
2180 | kfree(p); |
2181 | break; | |
2182 | ||
2183 | case SRP_OPT_MAX_SECT: | |
2184 | if (match_int(args, &token)) { | |
e0bda7d8 | 2185 | pr_warn("bad max sect parameter '%s'\n", p); |
aef9ec39 RD |
2186 | goto out; |
2187 | } | |
2188 | target->scsi_host->max_sectors = token; | |
2189 | break; | |
2190 | ||
52fb2b50 VP |
2191 | case SRP_OPT_MAX_CMD_PER_LUN: |
2192 | if (match_int(args, &token)) { | |
e0bda7d8 BVA |
2193 | pr_warn("bad max cmd_per_lun parameter '%s'\n", |
2194 | p); | |
52fb2b50 VP |
2195 | goto out; |
2196 | } | |
dd5e6e38 | 2197 | target->scsi_host->cmd_per_lun = min(token, SRP_CMD_SQ_SIZE); |
52fb2b50 VP |
2198 | break; |
2199 | ||
0c0450db R |
2200 | case SRP_OPT_IO_CLASS: |
2201 | if (match_hex(args, &token)) { | |
e0bda7d8 | 2202 | pr_warn("bad IO class parameter '%s'\n", p); |
0c0450db R |
2203 | goto out; |
2204 | } | |
2205 | if (token != SRP_REV10_IB_IO_CLASS && | |
2206 | token != SRP_REV16A_IB_IO_CLASS) { | |
e0bda7d8 BVA |
2207 | pr_warn("unknown IO class parameter value %x specified (use %x or %x).\n", |
2208 | token, SRP_REV10_IB_IO_CLASS, | |
2209 | SRP_REV16A_IB_IO_CLASS); | |
0c0450db R |
2210 | goto out; |
2211 | } | |
2212 | target->io_class = token; | |
2213 | break; | |
2214 | ||
01cb9bcb IR |
2215 | case SRP_OPT_INITIATOR_EXT: |
2216 | p = match_strdup(args); | |
a20f3a6d IR |
2217 | if (!p) { |
2218 | ret = -ENOMEM; | |
2219 | goto out; | |
2220 | } | |
01cb9bcb IR |
2221 | target->initiator_ext = cpu_to_be64(simple_strtoull(p, NULL, 16)); |
2222 | kfree(p); | |
2223 | break; | |
2224 | ||
49248644 DD |
2225 | case SRP_OPT_CMD_SG_ENTRIES: |
2226 | if (match_int(args, &token) || token < 1 || token > 255) { | |
e0bda7d8 BVA |
2227 | pr_warn("bad max cmd_sg_entries parameter '%s'\n", |
2228 | p); | |
49248644 DD |
2229 | goto out; |
2230 | } | |
2231 | target->cmd_sg_cnt = token; | |
2232 | break; | |
2233 | ||
c07d424d DD |
2234 | case SRP_OPT_ALLOW_EXT_SG: |
2235 | if (match_int(args, &token)) { | |
e0bda7d8 | 2236 | pr_warn("bad allow_ext_sg parameter '%s'\n", p); |
c07d424d DD |
2237 | goto out; |
2238 | } | |
2239 | target->allow_ext_sg = !!token; | |
2240 | break; | |
2241 | ||
2242 | case SRP_OPT_SG_TABLESIZE: | |
2243 | if (match_int(args, &token) || token < 1 || | |
2244 | token > SCSI_MAX_SG_CHAIN_SEGMENTS) { | |
e0bda7d8 BVA |
2245 | pr_warn("bad max sg_tablesize parameter '%s'\n", |
2246 | p); | |
c07d424d DD |
2247 | goto out; |
2248 | } | |
2249 | target->sg_tablesize = token; | |
2250 | break; | |
2251 | ||
4b5e5f41 BVA |
2252 | case SRP_OPT_COMP_VECTOR: |
2253 | if (match_int(args, &token) || token < 0) { | |
2254 | pr_warn("bad comp_vector parameter '%s'\n", p); | |
2255 | goto out; | |
2256 | } | |
2257 | target->comp_vector = token; | |
2258 | break; | |
2259 | ||
aef9ec39 | 2260 | default: |
e0bda7d8 BVA |
2261 | pr_warn("unknown parameter or missing value '%s' in target creation request\n", |
2262 | p); | |
aef9ec39 RD |
2263 | goto out; |
2264 | } | |
2265 | } | |
2266 | ||
2267 | if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL) | |
2268 | ret = 0; | |
2269 | else | |
2270 | for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i) | |
2271 | if ((srp_opt_tokens[i].token & SRP_OPT_ALL) && | |
2272 | !(srp_opt_tokens[i].token & opt_mask)) | |
e0bda7d8 BVA |
2273 | pr_warn("target creation request is missing parameter '%s'\n", |
2274 | srp_opt_tokens[i].pattern); | |
aef9ec39 RD |
2275 | |
2276 | out: | |
2277 | kfree(options); | |
2278 | return ret; | |
2279 | } | |
2280 | ||
ee959b00 TJ |
2281 | static ssize_t srp_create_target(struct device *dev, |
2282 | struct device_attribute *attr, | |
aef9ec39 RD |
2283 | const char *buf, size_t count) |
2284 | { | |
2285 | struct srp_host *host = | |
ee959b00 | 2286 | container_of(dev, struct srp_host, dev); |
aef9ec39 RD |
2287 | struct Scsi_Host *target_host; |
2288 | struct srp_target_port *target; | |
c07d424d DD |
2289 | struct ib_device *ibdev = host->srp_dev->dev; |
2290 | dma_addr_t dma_addr; | |
8f26c9ff | 2291 | int i, ret; |
aef9ec39 RD |
2292 | |
2293 | target_host = scsi_host_alloc(&srp_template, | |
2294 | sizeof (struct srp_target_port)); | |
2295 | if (!target_host) | |
2296 | return -ENOMEM; | |
2297 | ||
49248644 | 2298 | target_host->transportt = ib_srp_transport_template; |
fd1b6c4a BVA |
2299 | target_host->max_channel = 0; |
2300 | target_host->max_id = 1; | |
3c8edf0e AR |
2301 | target_host->max_lun = SRP_MAX_LUN; |
2302 | target_host->max_cmd_len = sizeof ((struct srp_cmd *) (void *) 0L)->cdb; | |
5f068992 | 2303 | |
aef9ec39 | 2304 | target = host_to_target(target_host); |
aef9ec39 | 2305 | |
49248644 DD |
2306 | target->io_class = SRP_REV16A_IB_IO_CLASS; |
2307 | target->scsi_host = target_host; | |
2308 | target->srp_host = host; | |
2309 | target->lkey = host->srp_dev->mr->lkey; | |
2310 | target->rkey = host->srp_dev->mr->rkey; | |
2311 | target->cmd_sg_cnt = cmd_sg_entries; | |
c07d424d DD |
2312 | target->sg_tablesize = indirect_sg_entries ? : cmd_sg_entries; |
2313 | target->allow_ext_sg = allow_ext_sg; | |
aef9ec39 | 2314 | |
aef9ec39 RD |
2315 | ret = srp_parse_options(buf, target); |
2316 | if (ret) | |
2317 | goto err; | |
2318 | ||
96fc248a BVA |
2319 | if (!srp_conn_unique(target->srp_host, target)) { |
2320 | shost_printk(KERN_INFO, target->scsi_host, | |
2321 | PFX "Already connected to target port with id_ext=%016llx;ioc_guid=%016llx;initiator_ext=%016llx\n", | |
2322 | be64_to_cpu(target->id_ext), | |
2323 | be64_to_cpu(target->ioc_guid), | |
2324 | be64_to_cpu(target->initiator_ext)); | |
2325 | ret = -EEXIST; | |
2326 | goto err; | |
2327 | } | |
2328 | ||
c07d424d DD |
2329 | if (!host->srp_dev->fmr_pool && !target->allow_ext_sg && |
2330 | target->cmd_sg_cnt < target->sg_tablesize) { | |
e0bda7d8 | 2331 | pr_warn("No FMR pool and no external indirect descriptors, limiting sg_tablesize to cmd_sg_cnt\n"); |
c07d424d DD |
2332 | target->sg_tablesize = target->cmd_sg_cnt; |
2333 | } | |
2334 | ||
2335 | target_host->sg_tablesize = target->sg_tablesize; | |
2336 | target->indirect_size = target->sg_tablesize * | |
2337 | sizeof (struct srp_direct_buf); | |
49248644 DD |
2338 | target->max_iu_len = sizeof (struct srp_cmd) + |
2339 | sizeof (struct srp_indirect_buf) + | |
2340 | target->cmd_sg_cnt * sizeof (struct srp_direct_buf); | |
2341 | ||
ef6c49d8 | 2342 | INIT_WORK(&target->remove_work, srp_remove_work); |
8f26c9ff DD |
2343 | spin_lock_init(&target->lock); |
2344 | INIT_LIST_HEAD(&target->free_tx); | |
2345 | INIT_LIST_HEAD(&target->free_reqs); | |
2346 | for (i = 0; i < SRP_CMD_SQ_SIZE; ++i) { | |
2347 | struct srp_request *req = &target->req_ring[i]; | |
2348 | ||
2349 | req->fmr_list = kmalloc(target->cmd_sg_cnt * sizeof (void *), | |
2350 | GFP_KERNEL); | |
2351 | req->map_page = kmalloc(SRP_FMR_SIZE * sizeof (void *), | |
2352 | GFP_KERNEL); | |
c07d424d DD |
2353 | req->indirect_desc = kmalloc(target->indirect_size, GFP_KERNEL); |
2354 | if (!req->fmr_list || !req->map_page || !req->indirect_desc) | |
8f26c9ff DD |
2355 | goto err_free_mem; |
2356 | ||
c07d424d DD |
2357 | dma_addr = ib_dma_map_single(ibdev, req->indirect_desc, |
2358 | target->indirect_size, | |
2359 | DMA_TO_DEVICE); | |
2360 | if (ib_dma_mapping_error(ibdev, dma_addr)) | |
2361 | goto err_free_mem; | |
2362 | ||
2363 | req->indirect_dma_addr = dma_addr; | |
8f26c9ff DD |
2364 | req->index = i; |
2365 | list_add_tail(&req->list, &target->free_reqs); | |
2366 | } | |
2367 | ||
c07d424d | 2368 | ib_query_gid(ibdev, host->port, 0, &target->path.sgid); |
aef9ec39 | 2369 | |
7aa54bd7 DD |
2370 | shost_printk(KERN_DEBUG, target->scsi_host, PFX |
2371 | "new target: id_ext %016llx ioc_guid %016llx pkey %04x " | |
5b095d98 | 2372 | "service_id %016llx dgid %pI6\n", |
aef9ec39 RD |
2373 | (unsigned long long) be64_to_cpu(target->id_ext), |
2374 | (unsigned long long) be64_to_cpu(target->ioc_guid), | |
2375 | be16_to_cpu(target->path.pkey), | |
2376 | (unsigned long long) be64_to_cpu(target->service_id), | |
8867cd7c | 2377 | target->path.dgid.raw); |
aef9ec39 RD |
2378 | |
2379 | ret = srp_create_target_ib(target); | |
2380 | if (ret) | |
8f26c9ff | 2381 | goto err_free_mem; |
aef9ec39 | 2382 | |
9fe4bcf4 DD |
2383 | ret = srp_new_cm_id(target); |
2384 | if (ret) | |
8f26c9ff | 2385 | goto err_free_ib; |
aef9ec39 RD |
2386 | |
2387 | ret = srp_connect_target(target); | |
2388 | if (ret) { | |
7aa54bd7 DD |
2389 | shost_printk(KERN_ERR, target->scsi_host, |
2390 | PFX "Connection failed\n"); | |
aef9ec39 RD |
2391 | goto err_cm_id; |
2392 | } | |
2393 | ||
2394 | ret = srp_add_target(host, target); | |
2395 | if (ret) | |
2396 | goto err_disconnect; | |
2397 | ||
2398 | return count; | |
2399 | ||
2400 | err_disconnect: | |
2401 | srp_disconnect_target(target); | |
2402 | ||
2403 | err_cm_id: | |
2404 | ib_destroy_cm_id(target->cm_id); | |
2405 | ||
8f26c9ff | 2406 | err_free_ib: |
aef9ec39 RD |
2407 | srp_free_target_ib(target); |
2408 | ||
8f26c9ff DD |
2409 | err_free_mem: |
2410 | srp_free_req_data(target); | |
2411 | ||
aef9ec39 RD |
2412 | err: |
2413 | scsi_host_put(target_host); | |
2414 | ||
2415 | return ret; | |
2416 | } | |
2417 | ||
ee959b00 | 2418 | static DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target); |
aef9ec39 | 2419 | |
ee959b00 TJ |
2420 | static ssize_t show_ibdev(struct device *dev, struct device_attribute *attr, |
2421 | char *buf) | |
aef9ec39 | 2422 | { |
ee959b00 | 2423 | struct srp_host *host = container_of(dev, struct srp_host, dev); |
aef9ec39 | 2424 | |
05321937 | 2425 | return sprintf(buf, "%s\n", host->srp_dev->dev->name); |
aef9ec39 RD |
2426 | } |
2427 | ||
ee959b00 | 2428 | static DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL); |
aef9ec39 | 2429 | |
ee959b00 TJ |
2430 | static ssize_t show_port(struct device *dev, struct device_attribute *attr, |
2431 | char *buf) | |
aef9ec39 | 2432 | { |
ee959b00 | 2433 | struct srp_host *host = container_of(dev, struct srp_host, dev); |
aef9ec39 RD |
2434 | |
2435 | return sprintf(buf, "%d\n", host->port); | |
2436 | } | |
2437 | ||
ee959b00 | 2438 | static DEVICE_ATTR(port, S_IRUGO, show_port, NULL); |
aef9ec39 | 2439 | |
f5358a17 | 2440 | static struct srp_host *srp_add_port(struct srp_device *device, u8 port) |
aef9ec39 RD |
2441 | { |
2442 | struct srp_host *host; | |
2443 | ||
2444 | host = kzalloc(sizeof *host, GFP_KERNEL); | |
2445 | if (!host) | |
2446 | return NULL; | |
2447 | ||
2448 | INIT_LIST_HEAD(&host->target_list); | |
b3589fd4 | 2449 | spin_lock_init(&host->target_lock); |
aef9ec39 | 2450 | init_completion(&host->released); |
05321937 | 2451 | host->srp_dev = device; |
aef9ec39 RD |
2452 | host->port = port; |
2453 | ||
ee959b00 TJ |
2454 | host->dev.class = &srp_class; |
2455 | host->dev.parent = device->dev->dma_device; | |
d927e38c | 2456 | dev_set_name(&host->dev, "srp-%s-%d", device->dev->name, port); |
aef9ec39 | 2457 | |
ee959b00 | 2458 | if (device_register(&host->dev)) |
f5358a17 | 2459 | goto free_host; |
ee959b00 | 2460 | if (device_create_file(&host->dev, &dev_attr_add_target)) |
aef9ec39 | 2461 | goto err_class; |
ee959b00 | 2462 | if (device_create_file(&host->dev, &dev_attr_ibdev)) |
aef9ec39 | 2463 | goto err_class; |
ee959b00 | 2464 | if (device_create_file(&host->dev, &dev_attr_port)) |
aef9ec39 RD |
2465 | goto err_class; |
2466 | ||
2467 | return host; | |
2468 | ||
2469 | err_class: | |
ee959b00 | 2470 | device_unregister(&host->dev); |
aef9ec39 | 2471 | |
f5358a17 | 2472 | free_host: |
aef9ec39 RD |
2473 | kfree(host); |
2474 | ||
2475 | return NULL; | |
2476 | } | |
2477 | ||
2478 | static void srp_add_one(struct ib_device *device) | |
2479 | { | |
f5358a17 RD |
2480 | struct srp_device *srp_dev; |
2481 | struct ib_device_attr *dev_attr; | |
2482 | struct ib_fmr_pool_param fmr_param; | |
aef9ec39 | 2483 | struct srp_host *host; |
be8b9814 | 2484 | int max_pages_per_fmr, fmr_page_shift, s, e, p; |
aef9ec39 | 2485 | |
f5358a17 RD |
2486 | dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL); |
2487 | if (!dev_attr) | |
cf311cd4 | 2488 | return; |
aef9ec39 | 2489 | |
f5358a17 | 2490 | if (ib_query_device(device, dev_attr)) { |
e0bda7d8 | 2491 | pr_warn("Query device failed for %s\n", device->name); |
f5358a17 RD |
2492 | goto free_attr; |
2493 | } | |
2494 | ||
2495 | srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL); | |
2496 | if (!srp_dev) | |
2497 | goto free_attr; | |
2498 | ||
2499 | /* | |
2500 | * Use the smallest page size supported by the HCA, down to a | |
8f26c9ff DD |
2501 | * minimum of 4096 bytes. We're unlikely to build large sglists |
2502 | * out of smaller entries. | |
f5358a17 | 2503 | */ |
8f26c9ff DD |
2504 | fmr_page_shift = max(12, ffs(dev_attr->page_size_cap) - 1); |
2505 | srp_dev->fmr_page_size = 1 << fmr_page_shift; | |
2506 | srp_dev->fmr_page_mask = ~((u64) srp_dev->fmr_page_size - 1); | |
2507 | srp_dev->fmr_max_size = srp_dev->fmr_page_size * SRP_FMR_SIZE; | |
f5358a17 RD |
2508 | |
2509 | INIT_LIST_HEAD(&srp_dev->dev_list); | |
2510 | ||
2511 | srp_dev->dev = device; | |
2512 | srp_dev->pd = ib_alloc_pd(device); | |
2513 | if (IS_ERR(srp_dev->pd)) | |
2514 | goto free_dev; | |
2515 | ||
2516 | srp_dev->mr = ib_get_dma_mr(srp_dev->pd, | |
2517 | IB_ACCESS_LOCAL_WRITE | | |
2518 | IB_ACCESS_REMOTE_READ | | |
2519 | IB_ACCESS_REMOTE_WRITE); | |
2520 | if (IS_ERR(srp_dev->mr)) | |
2521 | goto err_pd; | |
2522 | ||
be8b9814 DD |
2523 | for (max_pages_per_fmr = SRP_FMR_SIZE; |
2524 | max_pages_per_fmr >= SRP_FMR_MIN_SIZE; | |
2525 | max_pages_per_fmr /= 2, srp_dev->fmr_max_size /= 2) { | |
2526 | memset(&fmr_param, 0, sizeof fmr_param); | |
2527 | fmr_param.pool_size = SRP_FMR_POOL_SIZE; | |
2528 | fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE; | |
2529 | fmr_param.cache = 1; | |
2530 | fmr_param.max_pages_per_fmr = max_pages_per_fmr; | |
2531 | fmr_param.page_shift = fmr_page_shift; | |
2532 | fmr_param.access = (IB_ACCESS_LOCAL_WRITE | | |
2533 | IB_ACCESS_REMOTE_WRITE | | |
2534 | IB_ACCESS_REMOTE_READ); | |
2535 | ||
2536 | srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param); | |
2537 | if (!IS_ERR(srp_dev->fmr_pool)) | |
2538 | break; | |
2539 | } | |
2540 | ||
f5358a17 RD |
2541 | if (IS_ERR(srp_dev->fmr_pool)) |
2542 | srp_dev->fmr_pool = NULL; | |
aef9ec39 | 2543 | |
07ebafba | 2544 | if (device->node_type == RDMA_NODE_IB_SWITCH) { |
aef9ec39 RD |
2545 | s = 0; |
2546 | e = 0; | |
2547 | } else { | |
2548 | s = 1; | |
2549 | e = device->phys_port_cnt; | |
2550 | } | |
2551 | ||
2552 | for (p = s; p <= e; ++p) { | |
f5358a17 | 2553 | host = srp_add_port(srp_dev, p); |
aef9ec39 | 2554 | if (host) |
f5358a17 | 2555 | list_add_tail(&host->list, &srp_dev->dev_list); |
aef9ec39 RD |
2556 | } |
2557 | ||
f5358a17 RD |
2558 | ib_set_client_data(device, &srp_client, srp_dev); |
2559 | ||
2560 | goto free_attr; | |
2561 | ||
2562 | err_pd: | |
2563 | ib_dealloc_pd(srp_dev->pd); | |
2564 | ||
2565 | free_dev: | |
2566 | kfree(srp_dev); | |
2567 | ||
2568 | free_attr: | |
2569 | kfree(dev_attr); | |
aef9ec39 RD |
2570 | } |
2571 | ||
2572 | static void srp_remove_one(struct ib_device *device) | |
2573 | { | |
f5358a17 | 2574 | struct srp_device *srp_dev; |
aef9ec39 | 2575 | struct srp_host *host, *tmp_host; |
ef6c49d8 | 2576 | struct srp_target_port *target; |
aef9ec39 | 2577 | |
f5358a17 | 2578 | srp_dev = ib_get_client_data(device, &srp_client); |
1fe0cb84 DB |
2579 | if (!srp_dev) |
2580 | return; | |
aef9ec39 | 2581 | |
f5358a17 | 2582 | list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) { |
ee959b00 | 2583 | device_unregister(&host->dev); |
aef9ec39 RD |
2584 | /* |
2585 | * Wait for the sysfs entry to go away, so that no new | |
2586 | * target ports can be created. | |
2587 | */ | |
2588 | wait_for_completion(&host->released); | |
2589 | ||
2590 | /* | |
ef6c49d8 | 2591 | * Remove all target ports. |
aef9ec39 | 2592 | */ |
b3589fd4 | 2593 | spin_lock(&host->target_lock); |
ef6c49d8 BVA |
2594 | list_for_each_entry(target, &host->target_list, list) |
2595 | srp_queue_remove_work(target); | |
b3589fd4 | 2596 | spin_unlock(&host->target_lock); |
aef9ec39 RD |
2597 | |
2598 | /* | |
ef6c49d8 | 2599 | * Wait for target port removal tasks. |
aef9ec39 | 2600 | */ |
ef6c49d8 | 2601 | flush_workqueue(system_long_wq); |
aef9ec39 | 2602 | |
aef9ec39 RD |
2603 | kfree(host); |
2604 | } | |
2605 | ||
f5358a17 RD |
2606 | if (srp_dev->fmr_pool) |
2607 | ib_destroy_fmr_pool(srp_dev->fmr_pool); | |
2608 | ib_dereg_mr(srp_dev->mr); | |
2609 | ib_dealloc_pd(srp_dev->pd); | |
2610 | ||
2611 | kfree(srp_dev); | |
aef9ec39 RD |
2612 | } |
2613 | ||
3236822b | 2614 | static struct srp_function_template ib_srp_transport_functions = { |
dc1bdbd9 | 2615 | .rport_delete = srp_rport_delete, |
3236822b FT |
2616 | }; |
2617 | ||
aef9ec39 RD |
2618 | static int __init srp_init_module(void) |
2619 | { | |
2620 | int ret; | |
2621 | ||
dcb4cb85 | 2622 | BUILD_BUG_ON(FIELD_SIZEOF(struct ib_wc, wr_id) < sizeof(void *)); |
dd5e6e38 | 2623 | |
49248644 | 2624 | if (srp_sg_tablesize) { |
e0bda7d8 | 2625 | pr_warn("srp_sg_tablesize is deprecated, please use cmd_sg_entries\n"); |
49248644 DD |
2626 | if (!cmd_sg_entries) |
2627 | cmd_sg_entries = srp_sg_tablesize; | |
2628 | } | |
2629 | ||
2630 | if (!cmd_sg_entries) | |
2631 | cmd_sg_entries = SRP_DEF_SG_TABLESIZE; | |
2632 | ||
2633 | if (cmd_sg_entries > 255) { | |
e0bda7d8 | 2634 | pr_warn("Clamping cmd_sg_entries to 255\n"); |
49248644 | 2635 | cmd_sg_entries = 255; |
1e89a194 DD |
2636 | } |
2637 | ||
c07d424d DD |
2638 | if (!indirect_sg_entries) |
2639 | indirect_sg_entries = cmd_sg_entries; | |
2640 | else if (indirect_sg_entries < cmd_sg_entries) { | |
e0bda7d8 BVA |
2641 | pr_warn("Bumping up indirect_sg_entries to match cmd_sg_entries (%u)\n", |
2642 | cmd_sg_entries); | |
c07d424d DD |
2643 | indirect_sg_entries = cmd_sg_entries; |
2644 | } | |
2645 | ||
3236822b FT |
2646 | ib_srp_transport_template = |
2647 | srp_attach_transport(&ib_srp_transport_functions); | |
2648 | if (!ib_srp_transport_template) | |
2649 | return -ENOMEM; | |
2650 | ||
aef9ec39 RD |
2651 | ret = class_register(&srp_class); |
2652 | if (ret) { | |
e0bda7d8 | 2653 | pr_err("couldn't register class infiniband_srp\n"); |
3236822b | 2654 | srp_release_transport(ib_srp_transport_template); |
aef9ec39 RD |
2655 | return ret; |
2656 | } | |
2657 | ||
c1a0b23b MT |
2658 | ib_sa_register_client(&srp_sa_client); |
2659 | ||
aef9ec39 RD |
2660 | ret = ib_register_client(&srp_client); |
2661 | if (ret) { | |
e0bda7d8 | 2662 | pr_err("couldn't register IB client\n"); |
3236822b | 2663 | srp_release_transport(ib_srp_transport_template); |
c1a0b23b | 2664 | ib_sa_unregister_client(&srp_sa_client); |
aef9ec39 RD |
2665 | class_unregister(&srp_class); |
2666 | return ret; | |
2667 | } | |
2668 | ||
2669 | return 0; | |
2670 | } | |
2671 | ||
2672 | static void __exit srp_cleanup_module(void) | |
2673 | { | |
2674 | ib_unregister_client(&srp_client); | |
c1a0b23b | 2675 | ib_sa_unregister_client(&srp_sa_client); |
aef9ec39 | 2676 | class_unregister(&srp_class); |
3236822b | 2677 | srp_release_transport(ib_srp_transport_template); |
aef9ec39 RD |
2678 | } |
2679 | ||
2680 | module_init(srp_init_module); | |
2681 | module_exit(srp_cleanup_module); |