blob: 8257d5a2c8f8955124138cedca609f2719d37e1d [file] [log] [blame]
Roland Dreieraef9ec32005-11-02 14:07:13 -08001/*
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.
31 *
32 * $Id: ib_srp.c 3932 2005-11-01 17:19:29Z roland $
33 */
34
Roland Dreieraef9ec32005-11-02 14:07:13 -080035#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>
Tim Schmielaude259682006-01-08 01:02:05 -080042#include <linux/jiffies.h>
Roland Dreieraef9ec32005-11-02 14:07:13 -080043
44#include <asm/atomic.h>
45
46#include <scsi/scsi.h>
47#include <scsi/scsi_device.h>
48#include <scsi/scsi_dbg.h>
49#include <scsi/srp.h>
50
51#include <rdma/ib_cache.h>
52
53#include "ib_srp.h"
54
55#define DRV_NAME "ib_srp"
56#define PFX DRV_NAME ": "
57#define DRV_VERSION "0.2"
58#define DRV_RELDATE "November 1, 2005"
59
60MODULE_AUTHOR("Roland Dreier");
61MODULE_DESCRIPTION("InfiniBand SCSI RDMA Protocol initiator "
62 "v" DRV_VERSION " (" DRV_RELDATE ")");
63MODULE_LICENSE("Dual BSD/GPL");
64
Vu Pham74b0a152006-06-17 20:37:32 -070065static int srp_sg_tablesize = SRP_DEF_SG_TABLESIZE;
66static int srp_max_iu_len;
67
68module_param(srp_sg_tablesize, int, 0444);
69MODULE_PARM_DESC(srp_sg_tablesize,
70 "Max number of gather/scatter entries per I/O (default is 12)");
71
Roland Dreieraef9ec32005-11-02 14:07:13 -080072static int topspin_workarounds = 1;
73
74module_param(topspin_workarounds, int, 0444);
75MODULE_PARM_DESC(topspin_workarounds,
76 "Enable workarounds for Topspin/Cisco SRP target bugs if != 0");
77
78static const u8 topspin_oui[3] = { 0x00, 0x05, 0xad };
79
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -070080static int mellanox_workarounds = 1;
81
82module_param(mellanox_workarounds, int, 0444);
83MODULE_PARM_DESC(mellanox_workarounds,
84 "Enable workarounds for Mellanox SRP target bugs if != 0");
85
86static const u8 mellanox_oui[3] = { 0x00, 0x02, 0xc9 };
87
Roland Dreieraef9ec32005-11-02 14:07:13 -080088static void srp_add_one(struct ib_device *device);
89static void srp_remove_one(struct ib_device *device);
90static void srp_completion(struct ib_cq *cq, void *target_ptr);
91static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event);
92
93static struct ib_client srp_client = {
94 .name = "srp",
95 .add = srp_add_one,
96 .remove = srp_remove_one
97};
98
99static inline struct srp_target_port *host_to_target(struct Scsi_Host *host)
100{
101 return (struct srp_target_port *) host->hostdata;
102}
103
104static const char *srp_target_info(struct Scsi_Host *host)
105{
106 return host_to_target(host)->target_name;
107}
108
109static struct srp_iu *srp_alloc_iu(struct srp_host *host, size_t size,
110 gfp_t gfp_mask,
111 enum dma_data_direction direction)
112{
113 struct srp_iu *iu;
114
115 iu = kmalloc(sizeof *iu, gfp_mask);
116 if (!iu)
117 goto out;
118
119 iu->buf = kzalloc(size, gfp_mask);
120 if (!iu->buf)
121 goto out_free_iu;
122
Roland Dreierf5358a12006-06-17 20:37:29 -0700123 iu->dma = dma_map_single(host->dev->dev->dma_device,
124 iu->buf, size, direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800125 if (dma_mapping_error(iu->dma))
126 goto out_free_buf;
127
128 iu->size = size;
129 iu->direction = direction;
130
131 return iu;
132
133out_free_buf:
134 kfree(iu->buf);
135out_free_iu:
136 kfree(iu);
137out:
138 return NULL;
139}
140
141static void srp_free_iu(struct srp_host *host, struct srp_iu *iu)
142{
143 if (!iu)
144 return;
145
Roland Dreierf5358a12006-06-17 20:37:29 -0700146 dma_unmap_single(host->dev->dev->dma_device,
147 iu->dma, iu->size, iu->direction);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800148 kfree(iu->buf);
149 kfree(iu);
150}
151
152static void srp_qp_event(struct ib_event *event, void *context)
153{
154 printk(KERN_ERR PFX "QP event %d\n", event->event);
155}
156
157static int srp_init_qp(struct srp_target_port *target,
158 struct ib_qp *qp)
159{
160 struct ib_qp_attr *attr;
161 int ret;
162
163 attr = kmalloc(sizeof *attr, GFP_KERNEL);
164 if (!attr)
165 return -ENOMEM;
166
Roland Dreierf5358a12006-06-17 20:37:29 -0700167 ret = ib_find_cached_pkey(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800168 target->srp_host->port,
169 be16_to_cpu(target->path.pkey),
170 &attr->pkey_index);
171 if (ret)
172 goto out;
173
174 attr->qp_state = IB_QPS_INIT;
175 attr->qp_access_flags = (IB_ACCESS_REMOTE_READ |
176 IB_ACCESS_REMOTE_WRITE);
177 attr->port_num = target->srp_host->port;
178
179 ret = ib_modify_qp(qp, attr,
180 IB_QP_STATE |
181 IB_QP_PKEY_INDEX |
182 IB_QP_ACCESS_FLAGS |
183 IB_QP_PORT);
184
185out:
186 kfree(attr);
187 return ret;
188}
189
190static int srp_create_target_ib(struct srp_target_port *target)
191{
192 struct ib_qp_init_attr *init_attr;
193 int ret;
194
195 init_attr = kzalloc(sizeof *init_attr, GFP_KERNEL);
196 if (!init_attr)
197 return -ENOMEM;
198
Roland Dreierf5358a12006-06-17 20:37:29 -0700199 target->cq = ib_create_cq(target->srp_host->dev->dev, srp_completion,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800200 NULL, target, SRP_CQ_SIZE);
201 if (IS_ERR(target->cq)) {
202 ret = PTR_ERR(target->cq);
203 goto out;
204 }
205
206 ib_req_notify_cq(target->cq, IB_CQ_NEXT_COMP);
207
208 init_attr->event_handler = srp_qp_event;
209 init_attr->cap.max_send_wr = SRP_SQ_SIZE;
210 init_attr->cap.max_recv_wr = SRP_RQ_SIZE;
211 init_attr->cap.max_recv_sge = 1;
212 init_attr->cap.max_send_sge = 1;
213 init_attr->sq_sig_type = IB_SIGNAL_ALL_WR;
214 init_attr->qp_type = IB_QPT_RC;
215 init_attr->send_cq = target->cq;
216 init_attr->recv_cq = target->cq;
217
Roland Dreierf5358a12006-06-17 20:37:29 -0700218 target->qp = ib_create_qp(target->srp_host->dev->pd, init_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800219 if (IS_ERR(target->qp)) {
220 ret = PTR_ERR(target->qp);
221 ib_destroy_cq(target->cq);
222 goto out;
223 }
224
225 ret = srp_init_qp(target, target->qp);
226 if (ret) {
227 ib_destroy_qp(target->qp);
228 ib_destroy_cq(target->cq);
229 goto out;
230 }
231
232out:
233 kfree(init_attr);
234 return ret;
235}
236
237static void srp_free_target_ib(struct srp_target_port *target)
238{
239 int i;
240
241 ib_destroy_qp(target->qp);
242 ib_destroy_cq(target->cq);
243
244 for (i = 0; i < SRP_RQ_SIZE; ++i)
245 srp_free_iu(target->srp_host, target->rx_ring[i]);
246 for (i = 0; i < SRP_SQ_SIZE + 1; ++i)
247 srp_free_iu(target->srp_host, target->tx_ring[i]);
248}
249
250static void srp_path_rec_completion(int status,
251 struct ib_sa_path_rec *pathrec,
252 void *target_ptr)
253{
254 struct srp_target_port *target = target_ptr;
255
256 target->status = status;
257 if (status)
258 printk(KERN_ERR PFX "Got failed path rec status %d\n", status);
259 else
260 target->path = *pathrec;
261 complete(&target->done);
262}
263
264static int srp_lookup_path(struct srp_target_port *target)
265{
266 target->path.numb_path = 1;
267
268 init_completion(&target->done);
269
Roland Dreierf5358a12006-06-17 20:37:29 -0700270 target->path_query_id = ib_sa_path_rec_get(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800271 target->srp_host->port,
272 &target->path,
273 IB_SA_PATH_REC_DGID |
274 IB_SA_PATH_REC_SGID |
275 IB_SA_PATH_REC_NUMB_PATH |
276 IB_SA_PATH_REC_PKEY,
277 SRP_PATH_REC_TIMEOUT_MS,
278 GFP_KERNEL,
279 srp_path_rec_completion,
280 target, &target->path_query);
281 if (target->path_query_id < 0)
282 return target->path_query_id;
283
284 wait_for_completion(&target->done);
285
286 if (target->status < 0)
287 printk(KERN_WARNING PFX "Path record query failed\n");
288
289 return target->status;
290}
291
292static int srp_send_req(struct srp_target_port *target)
293{
294 struct {
295 struct ib_cm_req_param param;
296 struct srp_login_req priv;
297 } *req = NULL;
298 int status;
299
300 req = kzalloc(sizeof *req, GFP_KERNEL);
301 if (!req)
302 return -ENOMEM;
303
304 req->param.primary_path = &target->path;
305 req->param.alternate_path = NULL;
306 req->param.service_id = target->service_id;
307 req->param.qp_num = target->qp->qp_num;
308 req->param.qp_type = target->qp->qp_type;
309 req->param.private_data = &req->priv;
310 req->param.private_data_len = sizeof req->priv;
311 req->param.flow_control = 1;
312
313 get_random_bytes(&req->param.starting_psn, 4);
314 req->param.starting_psn &= 0xffffff;
315
316 /*
317 * Pick some arbitrary defaults here; we could make these
318 * module parameters if anyone cared about setting them.
319 */
320 req->param.responder_resources = 4;
321 req->param.remote_cm_response_timeout = 20;
322 req->param.local_cm_response_timeout = 20;
323 req->param.retry_count = 7;
324 req->param.rnr_retry_count = 7;
325 req->param.max_cm_retries = 15;
326
327 req->priv.opcode = SRP_LOGIN_REQ;
328 req->priv.tag = 0;
Vu Pham74b0a152006-06-17 20:37:32 -0700329 req->priv.req_it_iu_len = cpu_to_be32(srp_max_iu_len);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800330 req->priv.req_buf_fmt = cpu_to_be16(SRP_BUF_FORMAT_DIRECT |
331 SRP_BUF_FORMAT_INDIRECT);
Ramachandra K0c0450db2006-06-17 20:37:38 -0700332 /*
333 * In the published SRP specification (draft rev. 16a), the
334 * port identifier format is 8 bytes of ID extension followed
335 * by 8 bytes of GUID. Older drafts put the two halves in the
336 * opposite order, so that the GUID comes first.
337 *
338 * Targets conforming to these obsolete drafts can be
339 * recognized by the I/O Class they report.
340 */
341 if (target->io_class == SRP_REV10_IB_IO_CLASS) {
342 memcpy(req->priv.initiator_port_id,
343 target->srp_host->initiator_port_id + 8, 8);
344 memcpy(req->priv.initiator_port_id + 8,
345 target->srp_host->initiator_port_id, 8);
346 memcpy(req->priv.target_port_id, &target->ioc_guid, 8);
347 memcpy(req->priv.target_port_id + 8, &target->id_ext, 8);
348 } else {
349 memcpy(req->priv.initiator_port_id,
350 target->srp_host->initiator_port_id, 16);
351 memcpy(req->priv.target_port_id, &target->id_ext, 8);
352 memcpy(req->priv.target_port_id + 8, &target->ioc_guid, 8);
353 }
354
Roland Dreieraef9ec32005-11-02 14:07:13 -0800355 /*
356 * Topspin/Cisco SRP targets will reject our login unless we
357 * zero out the first 8 bytes of our initiator port ID. The
358 * second 8 bytes must be our local node GUID, but we always
359 * use that anyway.
360 */
361 if (topspin_workarounds && !memcmp(&target->ioc_guid, topspin_oui, 3)) {
362 printk(KERN_DEBUG PFX "Topspin/Cisco initiator port ID workaround "
363 "activated for target GUID %016llx\n",
364 (unsigned long long) be64_to_cpu(target->ioc_guid));
365 memset(req->priv.initiator_port_id, 0, 8);
366 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800367
368 status = ib_send_cm_req(target->cm_id, &req->param);
369
370 kfree(req);
371
372 return status;
373}
374
375static void srp_disconnect_target(struct srp_target_port *target)
376{
377 /* XXX should send SRP_I_LOGOUT request */
378
379 init_completion(&target->done);
Roland Dreiere6581052006-05-17 09:13:21 -0700380 if (ib_send_cm_dreq(target->cm_id, NULL, 0)) {
381 printk(KERN_DEBUG PFX "Sending CM DREQ failed\n");
382 return;
383 }
Roland Dreieraef9ec32005-11-02 14:07:13 -0800384 wait_for_completion(&target->done);
385}
386
387static void srp_remove_work(void *target_ptr)
388{
389 struct srp_target_port *target = target_ptr;
390
391 spin_lock_irq(target->scsi_host->host_lock);
392 if (target->state != SRP_TARGET_DEAD) {
393 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800394 return;
395 }
396 target->state = SRP_TARGET_REMOVED;
397 spin_unlock_irq(target->scsi_host->host_lock);
398
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700399 spin_lock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800400 list_del(&target->list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -0700401 spin_unlock(&target->srp_host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800402
403 scsi_remove_host(target->scsi_host);
404 ib_destroy_cm_id(target->cm_id);
405 srp_free_target_ib(target);
406 scsi_host_put(target->scsi_host);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800407}
408
409static int srp_connect_target(struct srp_target_port *target)
410{
411 int ret;
412
413 ret = srp_lookup_path(target);
414 if (ret)
415 return ret;
416
417 while (1) {
418 init_completion(&target->done);
419 ret = srp_send_req(target);
420 if (ret)
421 return ret;
422 wait_for_completion(&target->done);
423
424 /*
425 * The CM event handling code will set status to
426 * SRP_PORT_REDIRECT if we get a port redirect REJ
427 * back, or SRP_DLID_REDIRECT if we get a lid/qp
428 * redirect REJ back.
429 */
430 switch (target->status) {
431 case 0:
432 return 0;
433
434 case SRP_PORT_REDIRECT:
435 ret = srp_lookup_path(target);
436 if (ret)
437 return ret;
438 break;
439
440 case SRP_DLID_REDIRECT:
441 break;
442
443 default:
444 return target->status;
445 }
446 }
447}
448
Roland Dreierd945e1d2006-05-09 10:50:28 -0700449static void srp_unmap_data(struct scsi_cmnd *scmnd,
450 struct srp_target_port *target,
451 struct srp_request *req)
452{
453 struct scatterlist *scat;
454 int nents;
455
456 if (!scmnd->request_buffer ||
457 (scmnd->sc_data_direction != DMA_TO_DEVICE &&
458 scmnd->sc_data_direction != DMA_FROM_DEVICE))
459 return;
460
Roland Dreierf5358a12006-06-17 20:37:29 -0700461 if (req->fmr) {
462 ib_fmr_pool_unmap(req->fmr);
463 req->fmr = NULL;
464 }
465
Roland Dreierd945e1d2006-05-09 10:50:28 -0700466 /*
467 * This handling of non-SG commands can be killed when the
468 * SCSI midlayer no longer generates non-SG commands.
469 */
470 if (likely(scmnd->use_sg)) {
471 nents = scmnd->use_sg;
472 scat = scmnd->request_buffer;
473 } else {
474 nents = 1;
475 scat = &req->fake_sg;
476 }
477
Roland Dreierf5358a12006-06-17 20:37:29 -0700478 dma_unmap_sg(target->srp_host->dev->dev->dma_device, scat, nents,
Roland Dreierd945e1d2006-05-09 10:50:28 -0700479 scmnd->sc_data_direction);
480}
481
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700482static void srp_remove_req(struct srp_target_port *target, struct srp_request *req)
483{
484 srp_unmap_data(req->scmnd, target, req);
485 list_move_tail(&req->list, &target->free_reqs);
486}
487
488static void srp_reset_req(struct srp_target_port *target, struct srp_request *req)
489{
490 req->scmnd->result = DID_RESET << 16;
491 req->scmnd->scsi_done(req->scmnd);
492 srp_remove_req(target, req);
493}
494
Roland Dreieraef9ec32005-11-02 14:07:13 -0800495static int srp_reconnect_target(struct srp_target_port *target)
496{
497 struct ib_cm_id *new_cm_id;
498 struct ib_qp_attr qp_attr;
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700499 struct srp_request *req, *tmp;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800500 struct ib_wc wc;
501 int ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800502
503 spin_lock_irq(target->scsi_host->host_lock);
504 if (target->state != SRP_TARGET_LIVE) {
505 spin_unlock_irq(target->scsi_host->host_lock);
506 return -EAGAIN;
507 }
508 target->state = SRP_TARGET_CONNECTING;
509 spin_unlock_irq(target->scsi_host->host_lock);
510
511 srp_disconnect_target(target);
512 /*
513 * Now get a new local CM ID so that we avoid confusing the
514 * target in case things are really fouled up.
515 */
Roland Dreierf5358a12006-06-17 20:37:29 -0700516 new_cm_id = ib_create_cm_id(target->srp_host->dev->dev,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800517 srp_cm_handler, target);
518 if (IS_ERR(new_cm_id)) {
519 ret = PTR_ERR(new_cm_id);
520 goto err;
521 }
522 ib_destroy_cm_id(target->cm_id);
523 target->cm_id = new_cm_id;
524
525 qp_attr.qp_state = IB_QPS_RESET;
526 ret = ib_modify_qp(target->qp, &qp_attr, IB_QP_STATE);
527 if (ret)
528 goto err;
529
530 ret = srp_init_qp(target, target->qp);
531 if (ret)
532 goto err;
533
534 while (ib_poll_cq(target->cq, 1, &wc) > 0)
535 ; /* nothing */
536
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300537 spin_lock_irq(target->scsi_host->host_lock);
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -0700538 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
539 srp_reset_req(target, req);
Ishai Rabinovitzd916a8f2006-07-25 19:54:09 +0300540 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800541
542 target->rx_head = 0;
543 target->tx_head = 0;
544 target->tx_tail = 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800545
546 ret = srp_connect_target(target);
547 if (ret)
548 goto err;
549
550 spin_lock_irq(target->scsi_host->host_lock);
551 if (target->state == SRP_TARGET_CONNECTING) {
552 ret = 0;
553 target->state = SRP_TARGET_LIVE;
554 } else
555 ret = -EAGAIN;
556 spin_unlock_irq(target->scsi_host->host_lock);
557
558 return ret;
559
560err:
561 printk(KERN_ERR PFX "reconnect failed (%d), removing target port.\n", ret);
562
563 /*
564 * We couldn't reconnect, so kill our target port off.
565 * However, we have to defer the real removal because we might
566 * be in the context of the SCSI error handler now, which
567 * would deadlock if we call scsi_remove_host().
568 */
569 spin_lock_irq(target->scsi_host->host_lock);
570 if (target->state == SRP_TARGET_CONNECTING) {
571 target->state = SRP_TARGET_DEAD;
572 INIT_WORK(&target->work, srp_remove_work, target);
573 schedule_work(&target->work);
574 }
575 spin_unlock_irq(target->scsi_host->host_lock);
576
577 return ret;
578}
579
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700580static int srp_map_fmr(struct srp_target_port *target, struct scatterlist *scat,
Roland Dreierf5358a12006-06-17 20:37:29 -0700581 int sg_cnt, struct srp_request *req,
582 struct srp_direct_buf *buf)
583{
584 u64 io_addr = 0;
585 u64 *dma_pages;
586 u32 len;
587 int page_cnt;
588 int i, j;
589 int ret;
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700590 struct srp_device *dev = target->srp_host->dev;
Roland Dreierf5358a12006-06-17 20:37:29 -0700591
592 if (!dev->fmr_pool)
593 return -ENODEV;
594
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700595 if ((sg_dma_address(&scat[0]) & ~dev->fmr_page_mask) &&
596 mellanox_workarounds && !memcmp(&target->ioc_guid, mellanox_oui, 3))
597 return -EINVAL;
598
Roland Dreierf5358a12006-06-17 20:37:29 -0700599 len = page_cnt = 0;
600 for (i = 0; i < sg_cnt; ++i) {
601 if (sg_dma_address(&scat[i]) & ~dev->fmr_page_mask) {
602 if (i > 0)
603 return -EINVAL;
604 else
605 ++page_cnt;
606 }
607 if ((sg_dma_address(&scat[i]) + sg_dma_len(&scat[i])) &
608 ~dev->fmr_page_mask) {
609 if (i < sg_cnt - 1)
610 return -EINVAL;
611 else
612 ++page_cnt;
613 }
614
615 len += sg_dma_len(&scat[i]);
616 }
617
618 page_cnt += len >> dev->fmr_page_shift;
619 if (page_cnt > SRP_FMR_SIZE)
620 return -ENOMEM;
621
622 dma_pages = kmalloc(sizeof (u64) * page_cnt, GFP_ATOMIC);
623 if (!dma_pages)
624 return -ENOMEM;
625
626 page_cnt = 0;
627 for (i = 0; i < sg_cnt; ++i)
628 for (j = 0; j < sg_dma_len(&scat[i]); j += dev->fmr_page_size)
629 dma_pages[page_cnt++] =
630 (sg_dma_address(&scat[i]) & dev->fmr_page_mask) + j;
631
632 req->fmr = ib_fmr_pool_map_phys(dev->fmr_pool,
Michael S. Tsirkinadfaa882006-07-14 00:23:55 -0700633 dma_pages, page_cnt, io_addr);
Roland Dreierf5358a12006-06-17 20:37:29 -0700634 if (IS_ERR(req->fmr)) {
635 ret = PTR_ERR(req->fmr);
Vu Pham6583eb32006-07-14 00:23:53 -0700636 req->fmr = NULL;
Roland Dreierf5358a12006-06-17 20:37:29 -0700637 goto out;
638 }
639
640 buf->va = cpu_to_be64(sg_dma_address(&scat[0]) & ~dev->fmr_page_mask);
641 buf->key = cpu_to_be32(req->fmr->fmr->rkey);
642 buf->len = cpu_to_be32(len);
643
644 ret = 0;
645
646out:
647 kfree(dma_pages);
648
649 return ret;
650}
651
Roland Dreieraef9ec32005-11-02 14:07:13 -0800652static int srp_map_data(struct scsi_cmnd *scmnd, struct srp_target_port *target,
653 struct srp_request *req)
654{
Roland Dreiercf368712006-03-24 15:47:26 -0800655 struct scatterlist *scat;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800656 struct srp_cmd *cmd = req->cmd->buf;
Roland Dreiercf368712006-03-24 15:47:26 -0800657 int len, nents, count;
Roland Dreierf5358a12006-06-17 20:37:29 -0700658 u8 fmt = SRP_DATA_DESC_DIRECT;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800659
660 if (!scmnd->request_buffer || scmnd->sc_data_direction == DMA_NONE)
661 return sizeof (struct srp_cmd);
662
663 if (scmnd->sc_data_direction != DMA_FROM_DEVICE &&
664 scmnd->sc_data_direction != DMA_TO_DEVICE) {
665 printk(KERN_WARNING PFX "Unhandled data direction %d\n",
666 scmnd->sc_data_direction);
667 return -EINVAL;
668 }
669
Roland Dreiercf368712006-03-24 15:47:26 -0800670 /*
671 * This handling of non-SG commands can be killed when the
672 * SCSI midlayer no longer generates non-SG commands.
673 */
674 if (likely(scmnd->use_sg)) {
675 nents = scmnd->use_sg;
676 scat = scmnd->request_buffer;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800677 } else {
Roland Dreiercf368712006-03-24 15:47:26 -0800678 nents = 1;
679 scat = &req->fake_sg;
680 sg_init_one(scat, scmnd->request_buffer, scmnd->request_bufflen);
681 }
682
Roland Dreierf5358a12006-06-17 20:37:29 -0700683 count = dma_map_sg(target->srp_host->dev->dev->dma_device,
684 scat, nents, scmnd->sc_data_direction);
685
686 fmt = SRP_DATA_DESC_DIRECT;
687 len = sizeof (struct srp_cmd) + sizeof (struct srp_direct_buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800688
689 if (count == 1) {
Roland Dreierf5358a12006-06-17 20:37:29 -0700690 /*
691 * The midlayer only generated a single gather/scatter
692 * entry, or DMA mapping coalesced everything to a
693 * single entry. So a direct descriptor along with
694 * the DMA MR suffices.
695 */
Roland Dreieraef9ec32005-11-02 14:07:13 -0800696 struct srp_direct_buf *buf = (void *) cmd->add_data;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800697
Roland Dreiercf368712006-03-24 15:47:26 -0800698 buf->va = cpu_to_be64(sg_dma_address(scat));
Roland Dreierf5358a12006-06-17 20:37:29 -0700699 buf->key = cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800700 buf->len = cpu_to_be32(sg_dma_len(scat));
Ishai Rabinovitz559ce8f2006-08-03 10:35:43 -0700701 } else if (srp_map_fmr(target, scat, count, req,
Roland Dreierf5358a12006-06-17 20:37:29 -0700702 (void *) cmd->add_data)) {
703 /*
704 * FMR mapping failed, and the scatterlist has more
705 * than one entry. Generate an indirect memory
706 * descriptor.
707 */
Roland Dreiercf368712006-03-24 15:47:26 -0800708 struct srp_indirect_buf *buf = (void *) cmd->add_data;
709 u32 datalen = 0;
Roland Dreierf5358a12006-06-17 20:37:29 -0700710 int i;
Roland Dreiercf368712006-03-24 15:47:26 -0800711
712 fmt = SRP_DATA_DESC_INDIRECT;
Roland Dreierf5358a12006-06-17 20:37:29 -0700713 len = sizeof (struct srp_cmd) +
714 sizeof (struct srp_indirect_buf) +
715 count * sizeof (struct srp_direct_buf);
716
717 for (i = 0; i < count; ++i) {
718 buf->desc_list[i].va =
719 cpu_to_be64(sg_dma_address(&scat[i]));
720 buf->desc_list[i].key =
721 cpu_to_be32(target->srp_host->dev->mr->rkey);
722 buf->desc_list[i].len =
723 cpu_to_be32(sg_dma_len(&scat[i]));
724 datalen += sg_dma_len(&scat[i]);
725 }
Roland Dreiercf368712006-03-24 15:47:26 -0800726
727 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
728 cmd->data_out_desc_cnt = count;
729 else
730 cmd->data_in_desc_cnt = count;
731
Roland Dreierf5358a12006-06-17 20:37:29 -0700732 buf->table_desc.va =
733 cpu_to_be64(req->cmd->dma + sizeof *cmd + sizeof *buf);
Roland Dreiercf368712006-03-24 15:47:26 -0800734 buf->table_desc.key =
Roland Dreierf5358a12006-06-17 20:37:29 -0700735 cpu_to_be32(target->srp_host->dev->mr->rkey);
Roland Dreiercf368712006-03-24 15:47:26 -0800736 buf->table_desc.len =
737 cpu_to_be32(count * sizeof (struct srp_direct_buf));
738
Roland Dreiercf368712006-03-24 15:47:26 -0800739 buf->len = cpu_to_be32(datalen);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800740 }
741
742 if (scmnd->sc_data_direction == DMA_TO_DEVICE)
743 cmd->buf_fmt = fmt << 4;
744 else
745 cmd->buf_fmt = fmt;
746
Roland Dreieraef9ec32005-11-02 14:07:13 -0800747 return len;
748}
749
Roland Dreieraef9ec32005-11-02 14:07:13 -0800750static void srp_process_rsp(struct srp_target_port *target, struct srp_rsp *rsp)
751{
752 struct srp_request *req;
753 struct scsi_cmnd *scmnd;
754 unsigned long flags;
755 s32 delta;
756
757 delta = (s32) be32_to_cpu(rsp->req_lim_delta);
758
759 spin_lock_irqsave(target->scsi_host->host_lock, flags);
760
761 target->req_lim += delta;
762
763 req = &target->req_ring[rsp->tag & ~SRP_TAG_TSK_MGMT];
764
765 if (unlikely(rsp->tag & SRP_TAG_TSK_MGMT)) {
766 if (be32_to_cpu(rsp->resp_data_len) < 4)
767 req->tsk_status = -1;
768 else
769 req->tsk_status = rsp->data[3];
770 complete(&req->done);
771 } else {
Roland Dreierd945e1d2006-05-09 10:50:28 -0700772 scmnd = req->scmnd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800773 if (!scmnd)
774 printk(KERN_ERR "Null scmnd for RSP w/tag %016llx\n",
775 (unsigned long long) rsp->tag);
776 scmnd->result = rsp->status;
777
778 if (rsp->flags & SRP_RSP_FLAG_SNSVALID) {
779 memcpy(scmnd->sense_buffer, rsp->data +
780 be32_to_cpu(rsp->resp_data_len),
781 min_t(int, be32_to_cpu(rsp->sense_data_len),
782 SCSI_SENSE_BUFFERSIZE));
783 }
784
785 if (rsp->flags & (SRP_RSP_FLAG_DOOVER | SRP_RSP_FLAG_DOUNDER))
786 scmnd->resid = be32_to_cpu(rsp->data_out_res_cnt);
787 else if (rsp->flags & (SRP_RSP_FLAG_DIOVER | SRP_RSP_FLAG_DIUNDER))
788 scmnd->resid = be32_to_cpu(rsp->data_in_res_cnt);
789
Roland Dreieraef9ec32005-11-02 14:07:13 -0800790 if (!req->tsk_mgmt) {
Roland Dreieraef9ec32005-11-02 14:07:13 -0800791 scmnd->host_scribble = (void *) -1L;
792 scmnd->scsi_done(scmnd);
793
Roland Dreierd945e1d2006-05-09 10:50:28 -0700794 srp_remove_req(target, req);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800795 } else
796 req->cmd_done = 1;
797 }
798
799 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
800}
801
802static void srp_reconnect_work(void *target_ptr)
803{
804 struct srp_target_port *target = target_ptr;
805
806 srp_reconnect_target(target);
807}
808
809static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
810{
811 struct srp_iu *iu;
812 u8 opcode;
813
814 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
815
Roland Dreierf5358a12006-06-17 20:37:29 -0700816 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800817 target->max_ti_iu_len, DMA_FROM_DEVICE);
818
819 opcode = *(u8 *) iu->buf;
820
821 if (0) {
822 int i;
823
824 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
825
826 for (i = 0; i < wc->byte_len; ++i) {
827 if (i % 8 == 0)
828 printk(KERN_ERR " [%02x] ", i);
829 printk(" %02x", ((u8 *) iu->buf)[i]);
830 if ((i + 1) % 8 == 0)
831 printk("\n");
832 }
833
834 if (wc->byte_len % 8)
835 printk("\n");
836 }
837
838 switch (opcode) {
839 case SRP_RSP:
840 srp_process_rsp(target, iu->buf);
841 break;
842
843 case SRP_T_LOGOUT:
844 /* XXX Handle target logout */
845 printk(KERN_WARNING PFX "Got target logout request\n");
846 break;
847
848 default:
849 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
850 break;
851 }
852
Roland Dreierf5358a12006-06-17 20:37:29 -0700853 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800854 target->max_ti_iu_len, DMA_FROM_DEVICE);
855}
856
857static void srp_completion(struct ib_cq *cq, void *target_ptr)
858{
859 struct srp_target_port *target = target_ptr;
860 struct ib_wc wc;
861 unsigned long flags;
862
863 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
864 while (ib_poll_cq(cq, 1, &wc) > 0) {
865 if (wc.status) {
866 printk(KERN_ERR PFX "failed %s status %d\n",
867 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
868 wc.status);
869 spin_lock_irqsave(target->scsi_host->host_lock, flags);
870 if (target->state == SRP_TARGET_LIVE)
871 schedule_work(&target->work);
872 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
873 break;
874 }
875
876 if (wc.wr_id & SRP_OP_RECV)
877 srp_handle_recv(target, &wc);
878 else
879 ++target->tx_tail;
880 }
881}
882
883static int __srp_post_recv(struct srp_target_port *target)
884{
885 struct srp_iu *iu;
886 struct ib_sge list;
887 struct ib_recv_wr wr, *bad_wr;
888 unsigned int next;
889 int ret;
890
891 next = target->rx_head & (SRP_RQ_SIZE - 1);
892 wr.wr_id = next | SRP_OP_RECV;
893 iu = target->rx_ring[next];
894
895 list.addr = iu->dma;
896 list.length = iu->size;
Roland Dreierf5358a12006-06-17 20:37:29 -0700897 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800898
899 wr.next = NULL;
900 wr.sg_list = &list;
901 wr.num_sge = 1;
902
903 ret = ib_post_recv(target->qp, &wr, &bad_wr);
904 if (!ret)
905 ++target->rx_head;
906
907 return ret;
908}
909
910static int srp_post_recv(struct srp_target_port *target)
911{
912 unsigned long flags;
913 int ret;
914
915 spin_lock_irqsave(target->scsi_host->host_lock, flags);
916 ret = __srp_post_recv(target);
917 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
918
919 return ret;
920}
921
922/*
923 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800924 * req_lim and tx_head. Lock cannot be dropped between call here and
925 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800926 */
927static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
928{
929 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
930 return NULL;
931
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700932 if (unlikely(target->req_lim < 1))
933 ++target->zero_req_lim;
Roland Dreier47f2bce2005-11-15 00:19:21 -0800934
Roland Dreieraef9ec32005-11-02 14:07:13 -0800935 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
936}
937
938/*
939 * Must be called with target->scsi_host->host_lock held to protect
940 * req_lim and tx_head.
941 */
942static int __srp_post_send(struct srp_target_port *target,
943 struct srp_iu *iu, int len)
944{
945 struct ib_sge list;
946 struct ib_send_wr wr, *bad_wr;
947 int ret = 0;
948
Roland Dreieraef9ec32005-11-02 14:07:13 -0800949 list.addr = iu->dma;
950 list.length = len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700951 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800952
953 wr.next = NULL;
954 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
955 wr.sg_list = &list;
956 wr.num_sge = 1;
957 wr.opcode = IB_WR_SEND;
958 wr.send_flags = IB_SEND_SIGNALED;
959
960 ret = ib_post_send(target->qp, &wr, &bad_wr);
961
962 if (!ret) {
963 ++target->tx_head;
964 --target->req_lim;
965 }
966
967 return ret;
968}
969
970static int srp_queuecommand(struct scsi_cmnd *scmnd,
971 void (*done)(struct scsi_cmnd *))
972{
973 struct srp_target_port *target = host_to_target(scmnd->device->host);
974 struct srp_request *req;
975 struct srp_iu *iu;
976 struct srp_cmd *cmd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800977 int len;
978
979 if (target->state == SRP_TARGET_CONNECTING)
980 goto err;
981
982 if (target->state == SRP_TARGET_DEAD ||
983 target->state == SRP_TARGET_REMOVED) {
984 scmnd->result = DID_BAD_TARGET << 16;
985 done(scmnd);
986 return 0;
987 }
988
989 iu = __srp_get_tx_iu(target);
990 if (!iu)
991 goto err;
992
Roland Dreierf5358a12006-06-17 20:37:29 -0700993 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -0700994 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800995
Roland Dreierd945e1d2006-05-09 10:50:28 -0700996 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800997
998 scmnd->scsi_done = done;
999 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001000 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001001
1002 cmd = iu->buf;
1003 memset(cmd, 0, sizeof *cmd);
1004
1005 cmd->opcode = SRP_CMD;
1006 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001007 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001008 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
1009
Roland Dreieraef9ec32005-11-02 14:07:13 -08001010 req->scmnd = scmnd;
1011 req->cmd = iu;
1012 req->cmd_done = 0;
1013 req->tsk_mgmt = NULL;
1014
1015 len = srp_map_data(scmnd, target, req);
1016 if (len < 0) {
1017 printk(KERN_ERR PFX "Failed to map data\n");
1018 goto err;
1019 }
1020
1021 if (__srp_post_recv(target)) {
1022 printk(KERN_ERR PFX "Recv failed\n");
1023 goto err_unmap;
1024 }
1025
Roland Dreierf5358a12006-06-17 20:37:29 -07001026 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -07001027 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001028
1029 if (__srp_post_send(target, iu, len)) {
1030 printk(KERN_ERR PFX "Send failed\n");
1031 goto err_unmap;
1032 }
1033
Roland Dreierd945e1d2006-05-09 10:50:28 -07001034 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001035
1036 return 0;
1037
1038err_unmap:
1039 srp_unmap_data(scmnd, target, req);
1040
1041err:
1042 return SCSI_MLQUEUE_HOST_BUSY;
1043}
1044
1045static int srp_alloc_iu_bufs(struct srp_target_port *target)
1046{
1047 int i;
1048
1049 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1050 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1051 target->max_ti_iu_len,
1052 GFP_KERNEL, DMA_FROM_DEVICE);
1053 if (!target->rx_ring[i])
1054 goto err;
1055 }
1056
1057 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1058 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001059 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001060 GFP_KERNEL, DMA_TO_DEVICE);
1061 if (!target->tx_ring[i])
1062 goto err;
1063 }
1064
1065 return 0;
1066
1067err:
1068 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1069 srp_free_iu(target->srp_host, target->rx_ring[i]);
1070 target->rx_ring[i] = NULL;
1071 }
1072
1073 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1074 srp_free_iu(target->srp_host, target->tx_ring[i]);
1075 target->tx_ring[i] = NULL;
1076 }
1077
1078 return -ENOMEM;
1079}
1080
1081static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1082 struct ib_cm_event *event,
1083 struct srp_target_port *target)
1084{
1085 struct ib_class_port_info *cpi;
1086 int opcode;
1087
1088 switch (event->param.rej_rcvd.reason) {
1089 case IB_CM_REJ_PORT_CM_REDIRECT:
1090 cpi = event->param.rej_rcvd.ari;
1091 target->path.dlid = cpi->redirect_lid;
1092 target->path.pkey = cpi->redirect_pkey;
1093 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1094 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1095
1096 target->status = target->path.dlid ?
1097 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1098 break;
1099
1100 case IB_CM_REJ_PORT_REDIRECT:
1101 if (topspin_workarounds &&
1102 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1103 /*
1104 * Topspin/Cisco SRP gateways incorrectly send
1105 * reject reason code 25 when they mean 24
1106 * (port redirect).
1107 */
1108 memcpy(target->path.dgid.raw,
1109 event->param.rej_rcvd.ari, 16);
1110
1111 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1112 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1113 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1114
1115 target->status = SRP_PORT_REDIRECT;
1116 } else {
1117 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1118 target->status = -ECONNRESET;
1119 }
1120 break;
1121
1122 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1123 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1124 target->status = -ECONNRESET;
1125 break;
1126
1127 case IB_CM_REJ_CONSUMER_DEFINED:
1128 opcode = *(u8 *) event->private_data;
1129 if (opcode == SRP_LOGIN_REJ) {
1130 struct srp_login_rej *rej = event->private_data;
1131 u32 reason = be32_to_cpu(rej->reason);
1132
1133 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1134 printk(KERN_WARNING PFX
1135 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1136 else
1137 printk(KERN_WARNING PFX
1138 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1139 } else
1140 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1141 " opcode 0x%02x\n", opcode);
1142 target->status = -ECONNRESET;
1143 break;
1144
1145 default:
1146 printk(KERN_WARNING " REJ reason 0x%x\n",
1147 event->param.rej_rcvd.reason);
1148 target->status = -ECONNRESET;
1149 }
1150}
1151
1152static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1153{
1154 struct srp_target_port *target = cm_id->context;
1155 struct ib_qp_attr *qp_attr = NULL;
1156 int attr_mask = 0;
1157 int comp = 0;
1158 int opcode = 0;
1159
1160 switch (event->event) {
1161 case IB_CM_REQ_ERROR:
1162 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1163 comp = 1;
1164 target->status = -ECONNRESET;
1165 break;
1166
1167 case IB_CM_REP_RECEIVED:
1168 comp = 1;
1169 opcode = *(u8 *) event->private_data;
1170
1171 if (opcode == SRP_LOGIN_RSP) {
1172 struct srp_login_rsp *rsp = event->private_data;
1173
1174 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1175 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1176
1177 target->scsi_host->can_queue = min(target->req_lim,
1178 target->scsi_host->can_queue);
1179 } else {
1180 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1181 target->status = -ECONNRESET;
1182 break;
1183 }
1184
1185 target->status = srp_alloc_iu_bufs(target);
1186 if (target->status)
1187 break;
1188
1189 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1190 if (!qp_attr) {
1191 target->status = -ENOMEM;
1192 break;
1193 }
1194
1195 qp_attr->qp_state = IB_QPS_RTR;
1196 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1197 if (target->status)
1198 break;
1199
1200 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1201 if (target->status)
1202 break;
1203
1204 target->status = srp_post_recv(target);
1205 if (target->status)
1206 break;
1207
1208 qp_attr->qp_state = IB_QPS_RTS;
1209 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1210 if (target->status)
1211 break;
1212
1213 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1214 if (target->status)
1215 break;
1216
1217 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1218 if (target->status)
1219 break;
1220
1221 break;
1222
1223 case IB_CM_REJ_RECEIVED:
1224 printk(KERN_DEBUG PFX "REJ received\n");
1225 comp = 1;
1226
1227 srp_cm_rej_handler(cm_id, event, target);
1228 break;
1229
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001230 case IB_CM_DREQ_RECEIVED:
1231 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1232 if (ib_send_cm_drep(cm_id, NULL, 0))
1233 printk(KERN_ERR PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001234 break;
1235
1236 case IB_CM_TIMEWAIT_EXIT:
1237 printk(KERN_ERR PFX "connection closed\n");
1238
1239 comp = 1;
1240 target->status = 0;
1241 break;
1242
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001243 case IB_CM_MRA_RECEIVED:
1244 case IB_CM_DREQ_ERROR:
1245 case IB_CM_DREP_RECEIVED:
1246 break;
1247
Roland Dreieraef9ec32005-11-02 14:07:13 -08001248 default:
1249 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1250 break;
1251 }
1252
1253 if (comp)
1254 complete(&target->done);
1255
1256 kfree(qp_attr);
1257
1258 return 0;
1259}
1260
Roland Dreierd945e1d2006-05-09 10:50:28 -07001261static int srp_send_tsk_mgmt(struct srp_target_port *target,
1262 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001263{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001264 struct srp_iu *iu;
1265 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001266
1267 spin_lock_irq(target->scsi_host->host_lock);
1268
Roland Dreier1285b3a2006-03-03 15:47:25 -08001269 if (target->state == SRP_TARGET_DEAD ||
1270 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001271 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001272 goto out;
1273 }
1274
Roland Dreieraef9ec32005-11-02 14:07:13 -08001275 init_completion(&req->done);
1276
1277 iu = __srp_get_tx_iu(target);
1278 if (!iu)
1279 goto out;
1280
1281 tsk_mgmt = iu->buf;
1282 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1283
1284 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001285 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1286 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001287 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001288 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001289
1290 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1291 goto out;
1292
1293 req->tsk_mgmt = iu;
1294
1295 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001296
Roland Dreieraef9ec32005-11-02 14:07:13 -08001297 if (!wait_for_completion_timeout(&req->done,
1298 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001299 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001300
Roland Dreierd945e1d2006-05-09 10:50:28 -07001301 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001302
1303out:
1304 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001305 return -1;
1306}
1307
1308static int srp_find_req(struct srp_target_port *target,
1309 struct scsi_cmnd *scmnd,
1310 struct srp_request **req)
1311{
1312 if (scmnd->host_scribble == (void *) -1L)
1313 return -1;
1314
1315 *req = &target->req_ring[(long) scmnd->host_scribble];
1316
1317 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001318}
1319
1320static int srp_abort(struct scsi_cmnd *scmnd)
1321{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001322 struct srp_target_port *target = host_to_target(scmnd->device->host);
1323 struct srp_request *req;
1324 int ret = SUCCESS;
1325
Roland Dreieraef9ec32005-11-02 14:07:13 -08001326 printk(KERN_ERR "SRP abort called\n");
1327
Roland Dreierd945e1d2006-05-09 10:50:28 -07001328 if (srp_find_req(target, scmnd, &req))
1329 return FAILED;
1330 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1331 return FAILED;
1332
1333 spin_lock_irq(target->scsi_host->host_lock);
1334
1335 if (req->cmd_done) {
1336 srp_remove_req(target, req);
1337 scmnd->scsi_done(scmnd);
1338 } else if (!req->tsk_status) {
1339 srp_remove_req(target, req);
1340 scmnd->result = DID_ABORT << 16;
1341 } else
1342 ret = FAILED;
1343
1344 spin_unlock_irq(target->scsi_host->host_lock);
1345
1346 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001347}
1348
1349static int srp_reset_device(struct scsi_cmnd *scmnd)
1350{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001351 struct srp_target_port *target = host_to_target(scmnd->device->host);
1352 struct srp_request *req, *tmp;
1353
Roland Dreieraef9ec32005-11-02 14:07:13 -08001354 printk(KERN_ERR "SRP reset_device called\n");
1355
Roland Dreierd945e1d2006-05-09 10:50:28 -07001356 if (srp_find_req(target, scmnd, &req))
1357 return FAILED;
1358 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1359 return FAILED;
1360 if (req->tsk_status)
1361 return FAILED;
1362
1363 spin_lock_irq(target->scsi_host->host_lock);
1364
1365 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001366 if (req->scmnd->device == scmnd->device)
1367 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001368
1369 spin_unlock_irq(target->scsi_host->host_lock);
1370
1371 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001372}
1373
1374static int srp_reset_host(struct scsi_cmnd *scmnd)
1375{
1376 struct srp_target_port *target = host_to_target(scmnd->device->host);
1377 int ret = FAILED;
1378
1379 printk(KERN_ERR PFX "SRP reset_host called\n");
1380
1381 if (!srp_reconnect_target(target))
1382 ret = SUCCESS;
1383
1384 return ret;
1385}
1386
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001387static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1388{
1389 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1390
1391 if (target->state == SRP_TARGET_DEAD ||
1392 target->state == SRP_TARGET_REMOVED)
1393 return -ENODEV;
1394
1395 return sprintf(buf, "0x%016llx\n",
1396 (unsigned long long) be64_to_cpu(target->id_ext));
1397}
1398
1399static ssize_t show_ioc_guid(struct class_device *cdev, char *buf)
1400{
1401 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1402
1403 if (target->state == SRP_TARGET_DEAD ||
1404 target->state == SRP_TARGET_REMOVED)
1405 return -ENODEV;
1406
1407 return sprintf(buf, "0x%016llx\n",
1408 (unsigned long long) be64_to_cpu(target->ioc_guid));
1409}
1410
1411static ssize_t show_service_id(struct class_device *cdev, char *buf)
1412{
1413 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1414
1415 if (target->state == SRP_TARGET_DEAD ||
1416 target->state == SRP_TARGET_REMOVED)
1417 return -ENODEV;
1418
1419 return sprintf(buf, "0x%016llx\n",
1420 (unsigned long long) be64_to_cpu(target->service_id));
1421}
1422
1423static ssize_t show_pkey(struct class_device *cdev, char *buf)
1424{
1425 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1426
1427 if (target->state == SRP_TARGET_DEAD ||
1428 target->state == SRP_TARGET_REMOVED)
1429 return -ENODEV;
1430
1431 return sprintf(buf, "0x%04x\n", be16_to_cpu(target->path.pkey));
1432}
1433
1434static ssize_t show_dgid(struct class_device *cdev, char *buf)
1435{
1436 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1437
1438 if (target->state == SRP_TARGET_DEAD ||
1439 target->state == SRP_TARGET_REMOVED)
1440 return -ENODEV;
1441
1442 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1443 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1444 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1445 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1446 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1447 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1448 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1449 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1450 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1451}
1452
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001453static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1454{
1455 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1456
1457 if (target->state == SRP_TARGET_DEAD ||
1458 target->state == SRP_TARGET_REMOVED)
1459 return -ENODEV;
1460
1461 return sprintf(buf, "%d\n", target->zero_req_lim);
1462}
1463
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001464static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1465static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1466static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1467static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1468static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001469static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001470
1471static struct class_device_attribute *srp_host_attrs[] = {
1472 &class_device_attr_id_ext,
1473 &class_device_attr_ioc_guid,
1474 &class_device_attr_service_id,
1475 &class_device_attr_pkey,
1476 &class_device_attr_dgid,
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001477 &class_device_attr_zero_req_lim,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001478 NULL
1479};
1480
Roland Dreieraef9ec32005-11-02 14:07:13 -08001481static struct scsi_host_template srp_template = {
1482 .module = THIS_MODULE,
1483 .name = DRV_NAME,
1484 .info = srp_target_info,
1485 .queuecommand = srp_queuecommand,
1486 .eh_abort_handler = srp_abort,
1487 .eh_device_reset_handler = srp_reset_device,
1488 .eh_host_reset_handler = srp_reset_host,
1489 .can_queue = SRP_SQ_SIZE,
1490 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001491 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001492 .use_clustering = ENABLE_CLUSTERING,
1493 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001494};
1495
1496static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1497{
1498 sprintf(target->target_name, "SRP.T10:%016llX",
1499 (unsigned long long) be64_to_cpu(target->id_ext));
1500
Roland Dreierf5358a12006-06-17 20:37:29 -07001501 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001502 return -ENODEV;
1503
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001504 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001505 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001506 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001507
1508 target->state = SRP_TARGET_LIVE;
1509
Roland Dreieraef9ec32005-11-02 14:07:13 -08001510 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001511 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001512
1513 return 0;
1514}
1515
1516static void srp_release_class_dev(struct class_device *class_dev)
1517{
1518 struct srp_host *host =
1519 container_of(class_dev, struct srp_host, class_dev);
1520
1521 complete(&host->released);
1522}
1523
1524static struct class srp_class = {
1525 .name = "infiniband_srp",
1526 .release = srp_release_class_dev
1527};
1528
1529/*
1530 * Target ports are added by writing
1531 *
1532 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1533 * pkey=<P_Key>,service_id=<service ID>
1534 *
1535 * to the add_target sysfs attribute.
1536 */
1537enum {
1538 SRP_OPT_ERR = 0,
1539 SRP_OPT_ID_EXT = 1 << 0,
1540 SRP_OPT_IOC_GUID = 1 << 1,
1541 SRP_OPT_DGID = 1 << 2,
1542 SRP_OPT_PKEY = 1 << 3,
1543 SRP_OPT_SERVICE_ID = 1 << 4,
1544 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001545 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001546 SRP_OPT_IO_CLASS = 1 << 7,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001547 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1548 SRP_OPT_IOC_GUID |
1549 SRP_OPT_DGID |
1550 SRP_OPT_PKEY |
1551 SRP_OPT_SERVICE_ID),
1552};
1553
1554static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001555 { SRP_OPT_ID_EXT, "id_ext=%s" },
1556 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1557 { SRP_OPT_DGID, "dgid=%s" },
1558 { SRP_OPT_PKEY, "pkey=%x" },
1559 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1560 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1561 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001562 { SRP_OPT_IO_CLASS, "io_class=%x" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001563 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001564};
1565
1566static int srp_parse_options(const char *buf, struct srp_target_port *target)
1567{
1568 char *options, *sep_opt;
1569 char *p;
1570 char dgid[3];
1571 substring_t args[MAX_OPT_ARGS];
1572 int opt_mask = 0;
1573 int token;
1574 int ret = -EINVAL;
1575 int i;
1576
1577 options = kstrdup(buf, GFP_KERNEL);
1578 if (!options)
1579 return -ENOMEM;
1580
1581 sep_opt = options;
1582 while ((p = strsep(&sep_opt, ",")) != NULL) {
1583 if (!*p)
1584 continue;
1585
1586 token = match_token(p, srp_opt_tokens, args);
1587 opt_mask |= token;
1588
1589 switch (token) {
1590 case SRP_OPT_ID_EXT:
1591 p = match_strdup(args);
1592 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1593 kfree(p);
1594 break;
1595
1596 case SRP_OPT_IOC_GUID:
1597 p = match_strdup(args);
1598 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1599 kfree(p);
1600 break;
1601
1602 case SRP_OPT_DGID:
1603 p = match_strdup(args);
1604 if (strlen(p) != 32) {
1605 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001606 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001607 goto out;
1608 }
1609
1610 for (i = 0; i < 16; ++i) {
1611 strlcpy(dgid, p + i * 2, 3);
1612 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1613 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001614 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001615 break;
1616
1617 case SRP_OPT_PKEY:
1618 if (match_hex(args, &token)) {
1619 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1620 goto out;
1621 }
1622 target->path.pkey = cpu_to_be16(token);
1623 break;
1624
1625 case SRP_OPT_SERVICE_ID:
1626 p = match_strdup(args);
1627 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1628 kfree(p);
1629 break;
1630
1631 case SRP_OPT_MAX_SECT:
1632 if (match_int(args, &token)) {
1633 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1634 goto out;
1635 }
1636 target->scsi_host->max_sectors = token;
1637 break;
1638
Vu Pham52fb2b502006-06-17 20:37:31 -07001639 case SRP_OPT_MAX_CMD_PER_LUN:
1640 if (match_int(args, &token)) {
1641 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1642 goto out;
1643 }
1644 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1645 break;
1646
Ramachandra K0c0450db2006-06-17 20:37:38 -07001647 case SRP_OPT_IO_CLASS:
1648 if (match_hex(args, &token)) {
1649 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1650 goto out;
1651 }
1652 if (token != SRP_REV10_IB_IO_CLASS &&
1653 token != SRP_REV16A_IB_IO_CLASS) {
1654 printk(KERN_WARNING PFX "unknown IO class parameter value"
1655 " %x specified (use %x or %x).\n",
1656 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1657 goto out;
1658 }
1659 target->io_class = token;
1660 break;
1661
Roland Dreieraef9ec32005-11-02 14:07:13 -08001662 default:
1663 printk(KERN_WARNING PFX "unknown parameter or missing value "
1664 "'%s' in target creation request\n", p);
1665 goto out;
1666 }
1667 }
1668
1669 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1670 ret = 0;
1671 else
1672 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1673 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1674 !(srp_opt_tokens[i].token & opt_mask))
1675 printk(KERN_WARNING PFX "target creation request is "
1676 "missing parameter '%s'\n",
1677 srp_opt_tokens[i].pattern);
1678
1679out:
1680 kfree(options);
1681 return ret;
1682}
1683
1684static ssize_t srp_create_target(struct class_device *class_dev,
1685 const char *buf, size_t count)
1686{
1687 struct srp_host *host =
1688 container_of(class_dev, struct srp_host, class_dev);
1689 struct Scsi_Host *target_host;
1690 struct srp_target_port *target;
1691 int ret;
1692 int i;
1693
1694 target_host = scsi_host_alloc(&srp_template,
1695 sizeof (struct srp_target_port));
1696 if (!target_host)
1697 return -ENOMEM;
1698
Roland Dreier5f068992005-11-11 14:06:01 -08001699 target_host->max_lun = SRP_MAX_LUN;
1700
Roland Dreieraef9ec32005-11-02 14:07:13 -08001701 target = host_to_target(target_host);
1702 memset(target, 0, sizeof *target);
1703
Ramachandra K0c0450db2006-06-17 20:37:38 -07001704 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001705 target->scsi_host = target_host;
1706 target->srp_host = host;
1707
1708 INIT_WORK(&target->work, srp_reconnect_work, target);
1709
Roland Dreierd945e1d2006-05-09 10:50:28 -07001710 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001711 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001712 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1713 target->req_ring[i].index = i;
1714 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1715 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001716
1717 ret = srp_parse_options(buf, target);
1718 if (ret)
1719 goto err;
1720
Roland Dreierf5358a12006-06-17 20:37:29 -07001721 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001722
1723 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1724 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1725 (unsigned long long) be64_to_cpu(target->id_ext),
1726 (unsigned long long) be64_to_cpu(target->ioc_guid),
1727 be16_to_cpu(target->path.pkey),
1728 (unsigned long long) be64_to_cpu(target->service_id),
1729 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1730 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1731 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1732 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1733 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1734 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1735 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1736 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1737
1738 ret = srp_create_target_ib(target);
1739 if (ret)
1740 goto err;
1741
Roland Dreierf5358a12006-06-17 20:37:29 -07001742 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001743 if (IS_ERR(target->cm_id)) {
1744 ret = PTR_ERR(target->cm_id);
1745 goto err_free;
1746 }
1747
1748 ret = srp_connect_target(target);
1749 if (ret) {
1750 printk(KERN_ERR PFX "Connection failed\n");
1751 goto err_cm_id;
1752 }
1753
1754 ret = srp_add_target(host, target);
1755 if (ret)
1756 goto err_disconnect;
1757
1758 return count;
1759
1760err_disconnect:
1761 srp_disconnect_target(target);
1762
1763err_cm_id:
1764 ib_destroy_cm_id(target->cm_id);
1765
1766err_free:
1767 srp_free_target_ib(target);
1768
1769err:
1770 scsi_host_put(target_host);
1771
1772 return ret;
1773}
1774
1775static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1776
1777static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1778{
1779 struct srp_host *host =
1780 container_of(class_dev, struct srp_host, class_dev);
1781
Roland Dreierf5358a12006-06-17 20:37:29 -07001782 return sprintf(buf, "%s\n", host->dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001783}
1784
1785static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1786
1787static ssize_t show_port(struct class_device *class_dev, char *buf)
1788{
1789 struct srp_host *host =
1790 container_of(class_dev, struct srp_host, class_dev);
1791
1792 return sprintf(buf, "%d\n", host->port);
1793}
1794
1795static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1796
Roland Dreierf5358a12006-06-17 20:37:29 -07001797static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001798{
1799 struct srp_host *host;
1800
1801 host = kzalloc(sizeof *host, GFP_KERNEL);
1802 if (!host)
1803 return NULL;
1804
1805 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001806 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001807 init_completion(&host->released);
1808 host->dev = device;
1809 host->port = port;
1810
1811 host->initiator_port_id[7] = port;
Roland Dreierf5358a12006-06-17 20:37:29 -07001812 memcpy(host->initiator_port_id + 8, &device->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001813
1814 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001815 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001816 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001817 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001818
1819 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001820 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001821 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1822 goto err_class;
1823 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1824 goto err_class;
1825 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1826 goto err_class;
1827
1828 return host;
1829
1830err_class:
1831 class_device_unregister(&host->class_dev);
1832
Roland Dreierf5358a12006-06-17 20:37:29 -07001833free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001834 kfree(host);
1835
1836 return NULL;
1837}
1838
1839static void srp_add_one(struct ib_device *device)
1840{
Roland Dreierf5358a12006-06-17 20:37:29 -07001841 struct srp_device *srp_dev;
1842 struct ib_device_attr *dev_attr;
1843 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001844 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001845 int s, e, p;
1846
Roland Dreierf5358a12006-06-17 20:37:29 -07001847 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1848 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001849 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001850
Roland Dreierf5358a12006-06-17 20:37:29 -07001851 if (ib_query_device(device, dev_attr)) {
1852 printk(KERN_WARNING PFX "Query device failed for %s\n",
1853 device->name);
1854 goto free_attr;
1855 }
1856
1857 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1858 if (!srp_dev)
1859 goto free_attr;
1860
1861 /*
1862 * Use the smallest page size supported by the HCA, down to a
1863 * minimum of 512 bytes (which is the smallest sector that a
1864 * SCSI command will ever carry).
1865 */
1866 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1867 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
1868 srp_dev->fmr_page_mask = ~((unsigned long) srp_dev->fmr_page_size - 1);
1869
1870 INIT_LIST_HEAD(&srp_dev->dev_list);
1871
1872 srp_dev->dev = device;
1873 srp_dev->pd = ib_alloc_pd(device);
1874 if (IS_ERR(srp_dev->pd))
1875 goto free_dev;
1876
1877 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1878 IB_ACCESS_LOCAL_WRITE |
1879 IB_ACCESS_REMOTE_READ |
1880 IB_ACCESS_REMOTE_WRITE);
1881 if (IS_ERR(srp_dev->mr))
1882 goto err_pd;
1883
1884 memset(&fmr_param, 0, sizeof fmr_param);
1885 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1886 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1887 fmr_param.cache = 1;
1888 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1889 fmr_param.page_shift = srp_dev->fmr_page_shift;
1890 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1891 IB_ACCESS_REMOTE_WRITE |
1892 IB_ACCESS_REMOTE_READ);
1893
1894 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1895 if (IS_ERR(srp_dev->fmr_pool))
1896 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001897
1898 if (device->node_type == IB_NODE_SWITCH) {
1899 s = 0;
1900 e = 0;
1901 } else {
1902 s = 1;
1903 e = device->phys_port_cnt;
1904 }
1905
1906 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001907 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001908 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07001909 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001910 }
1911
Roland Dreierf5358a12006-06-17 20:37:29 -07001912 ib_set_client_data(device, &srp_client, srp_dev);
1913
1914 goto free_attr;
1915
1916err_pd:
1917 ib_dealloc_pd(srp_dev->pd);
1918
1919free_dev:
1920 kfree(srp_dev);
1921
1922free_attr:
1923 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001924}
1925
1926static void srp_remove_one(struct ib_device *device)
1927{
Roland Dreierf5358a12006-06-17 20:37:29 -07001928 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001929 struct srp_host *host, *tmp_host;
1930 LIST_HEAD(target_list);
1931 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001932
Roland Dreierf5358a12006-06-17 20:37:29 -07001933 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001934
Roland Dreierf5358a12006-06-17 20:37:29 -07001935 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001936 class_device_unregister(&host->class_dev);
1937 /*
1938 * Wait for the sysfs entry to go away, so that no new
1939 * target ports can be created.
1940 */
1941 wait_for_completion(&host->released);
1942
1943 /*
1944 * Mark all target ports as removed, so we stop queueing
1945 * commands and don't try to reconnect.
1946 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001947 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07001948 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07001949 spin_lock_irq(target->scsi_host->host_lock);
1950 target->state = SRP_TARGET_REMOVED;
1951 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001952 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001953 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001954
1955 /*
1956 * Wait for any reconnection tasks that may have
1957 * started before we marked our target ports as
1958 * removed, and any target port removal tasks.
1959 */
1960 flush_scheduled_work();
1961
1962 list_for_each_entry_safe(target, tmp_target,
1963 &host->target_list, list) {
1964 scsi_remove_host(target->scsi_host);
1965 srp_disconnect_target(target);
1966 ib_destroy_cm_id(target->cm_id);
1967 srp_free_target_ib(target);
1968 scsi_host_put(target->scsi_host);
1969 }
1970
Roland Dreieraef9ec32005-11-02 14:07:13 -08001971 kfree(host);
1972 }
1973
Roland Dreierf5358a12006-06-17 20:37:29 -07001974 if (srp_dev->fmr_pool)
1975 ib_destroy_fmr_pool(srp_dev->fmr_pool);
1976 ib_dereg_mr(srp_dev->mr);
1977 ib_dealloc_pd(srp_dev->pd);
1978
1979 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001980}
1981
1982static int __init srp_init_module(void)
1983{
1984 int ret;
1985
Vu Pham74b0a152006-06-17 20:37:32 -07001986 srp_template.sg_tablesize = srp_sg_tablesize;
1987 srp_max_iu_len = (sizeof (struct srp_cmd) +
1988 sizeof (struct srp_indirect_buf) +
1989 srp_sg_tablesize * 16);
1990
Roland Dreieraef9ec32005-11-02 14:07:13 -08001991 ret = class_register(&srp_class);
1992 if (ret) {
1993 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
1994 return ret;
1995 }
1996
1997 ret = ib_register_client(&srp_client);
1998 if (ret) {
1999 printk(KERN_ERR PFX "couldn't register IB client\n");
2000 class_unregister(&srp_class);
2001 return ret;
2002 }
2003
2004 return 0;
2005}
2006
2007static void __exit srp_cleanup_module(void)
2008{
2009 ib_unregister_client(&srp_client);
2010 class_unregister(&srp_class);
2011}
2012
2013module_init(srp_init_module);
2014module_exit(srp_cleanup_module);