blob: 61c13d1e050636f35fe94fd1034861959173b557 [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 /*
Roland Dreier3cd96562006-09-22 15:22:46 -0700333 * In the published SRP specification (draft rev. 16a), the
Ramachandra K0c0450db2006-06-17 20:37:38 -0700334 * 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
Roland Dreieraef9ec32005-11-02 14:07:13 -0800802static void srp_handle_recv(struct srp_target_port *target, struct ib_wc *wc)
803{
804 struct srp_iu *iu;
805 u8 opcode;
806
807 iu = target->rx_ring[wc->wr_id & ~SRP_OP_RECV];
808
Roland Dreierf5358a12006-06-17 20:37:29 -0700809 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800810 target->max_ti_iu_len, DMA_FROM_DEVICE);
811
812 opcode = *(u8 *) iu->buf;
813
814 if (0) {
815 int i;
816
817 printk(KERN_ERR PFX "recv completion, opcode 0x%02x\n", opcode);
818
819 for (i = 0; i < wc->byte_len; ++i) {
820 if (i % 8 == 0)
821 printk(KERN_ERR " [%02x] ", i);
822 printk(" %02x", ((u8 *) iu->buf)[i]);
823 if ((i + 1) % 8 == 0)
824 printk("\n");
825 }
826
827 if (wc->byte_len % 8)
828 printk("\n");
829 }
830
831 switch (opcode) {
832 case SRP_RSP:
833 srp_process_rsp(target, iu->buf);
834 break;
835
836 case SRP_T_LOGOUT:
837 /* XXX Handle target logout */
838 printk(KERN_WARNING PFX "Got target logout request\n");
839 break;
840
841 default:
842 printk(KERN_WARNING PFX "Unhandled SRP opcode 0x%02x\n", opcode);
843 break;
844 }
845
Roland Dreierf5358a12006-06-17 20:37:29 -0700846 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Roland Dreieraef9ec32005-11-02 14:07:13 -0800847 target->max_ti_iu_len, DMA_FROM_DEVICE);
848}
849
850static void srp_completion(struct ib_cq *cq, void *target_ptr)
851{
852 struct srp_target_port *target = target_ptr;
853 struct ib_wc wc;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800854
855 ib_req_notify_cq(cq, IB_CQ_NEXT_COMP);
856 while (ib_poll_cq(cq, 1, &wc) > 0) {
857 if (wc.status) {
858 printk(KERN_ERR PFX "failed %s status %d\n",
859 wc.wr_id & SRP_OP_RECV ? "receive" : "send",
860 wc.status);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800861 break;
862 }
863
864 if (wc.wr_id & SRP_OP_RECV)
865 srp_handle_recv(target, &wc);
866 else
867 ++target->tx_tail;
868 }
869}
870
871static int __srp_post_recv(struct srp_target_port *target)
872{
873 struct srp_iu *iu;
874 struct ib_sge list;
875 struct ib_recv_wr wr, *bad_wr;
876 unsigned int next;
877 int ret;
878
879 next = target->rx_head & (SRP_RQ_SIZE - 1);
880 wr.wr_id = next | SRP_OP_RECV;
881 iu = target->rx_ring[next];
882
883 list.addr = iu->dma;
884 list.length = iu->size;
Roland Dreierf5358a12006-06-17 20:37:29 -0700885 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800886
887 wr.next = NULL;
888 wr.sg_list = &list;
889 wr.num_sge = 1;
890
891 ret = ib_post_recv(target->qp, &wr, &bad_wr);
892 if (!ret)
893 ++target->rx_head;
894
895 return ret;
896}
897
898static int srp_post_recv(struct srp_target_port *target)
899{
900 unsigned long flags;
901 int ret;
902
903 spin_lock_irqsave(target->scsi_host->host_lock, flags);
904 ret = __srp_post_recv(target);
905 spin_unlock_irqrestore(target->scsi_host->host_lock, flags);
906
907 return ret;
908}
909
910/*
911 * Must be called with target->scsi_host->host_lock held to protect
Roland Dreier47f2bce2005-11-15 00:19:21 -0800912 * req_lim and tx_head. Lock cannot be dropped between call here and
913 * call to __srp_post_send().
Roland Dreieraef9ec32005-11-02 14:07:13 -0800914 */
915static struct srp_iu *__srp_get_tx_iu(struct srp_target_port *target)
916{
917 if (target->tx_head - target->tx_tail >= SRP_SQ_SIZE)
918 return NULL;
919
Roland Dreier6bfa24f2006-06-17 20:37:33 -0700920 if (unlikely(target->req_lim < 1))
921 ++target->zero_req_lim;
Roland Dreier47f2bce2005-11-15 00:19:21 -0800922
Roland Dreieraef9ec32005-11-02 14:07:13 -0800923 return target->tx_ring[target->tx_head & SRP_SQ_SIZE];
924}
925
926/*
927 * Must be called with target->scsi_host->host_lock held to protect
928 * req_lim and tx_head.
929 */
930static int __srp_post_send(struct srp_target_port *target,
931 struct srp_iu *iu, int len)
932{
933 struct ib_sge list;
934 struct ib_send_wr wr, *bad_wr;
935 int ret = 0;
936
Roland Dreieraef9ec32005-11-02 14:07:13 -0800937 list.addr = iu->dma;
938 list.length = len;
Roland Dreierf5358a12006-06-17 20:37:29 -0700939 list.lkey = target->srp_host->dev->mr->lkey;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800940
941 wr.next = NULL;
942 wr.wr_id = target->tx_head & SRP_SQ_SIZE;
943 wr.sg_list = &list;
944 wr.num_sge = 1;
945 wr.opcode = IB_WR_SEND;
946 wr.send_flags = IB_SEND_SIGNALED;
947
948 ret = ib_post_send(target->qp, &wr, &bad_wr);
949
950 if (!ret) {
951 ++target->tx_head;
952 --target->req_lim;
953 }
954
955 return ret;
956}
957
958static int srp_queuecommand(struct scsi_cmnd *scmnd,
959 void (*done)(struct scsi_cmnd *))
960{
961 struct srp_target_port *target = host_to_target(scmnd->device->host);
962 struct srp_request *req;
963 struct srp_iu *iu;
964 struct srp_cmd *cmd;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800965 int len;
966
967 if (target->state == SRP_TARGET_CONNECTING)
968 goto err;
969
970 if (target->state == SRP_TARGET_DEAD ||
971 target->state == SRP_TARGET_REMOVED) {
972 scmnd->result = DID_BAD_TARGET << 16;
973 done(scmnd);
974 return 0;
975 }
976
977 iu = __srp_get_tx_iu(target);
978 if (!iu)
979 goto err;
980
Roland Dreierf5358a12006-06-17 20:37:29 -0700981 dma_sync_single_for_cpu(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -0700982 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800983
Roland Dreierd945e1d2006-05-09 10:50:28 -0700984 req = list_entry(target->free_reqs.next, struct srp_request, list);
Roland Dreieraef9ec32005-11-02 14:07:13 -0800985
986 scmnd->scsi_done = done;
987 scmnd->result = 0;
Roland Dreierd945e1d2006-05-09 10:50:28 -0700988 scmnd->host_scribble = (void *) (long) req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800989
990 cmd = iu->buf;
991 memset(cmd, 0, sizeof *cmd);
992
993 cmd->opcode = SRP_CMD;
994 cmd->lun = cpu_to_be64((u64) scmnd->device->lun << 48);
Roland Dreierd945e1d2006-05-09 10:50:28 -0700995 cmd->tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -0800996 memcpy(cmd->cdb, scmnd->cmnd, scmnd->cmd_len);
997
Roland Dreieraef9ec32005-11-02 14:07:13 -0800998 req->scmnd = scmnd;
999 req->cmd = iu;
1000 req->cmd_done = 0;
1001 req->tsk_mgmt = NULL;
1002
1003 len = srp_map_data(scmnd, target, req);
1004 if (len < 0) {
1005 printk(KERN_ERR PFX "Failed to map data\n");
1006 goto err;
1007 }
1008
1009 if (__srp_post_recv(target)) {
1010 printk(KERN_ERR PFX "Recv failed\n");
1011 goto err_unmap;
1012 }
1013
Roland Dreierf5358a12006-06-17 20:37:29 -07001014 dma_sync_single_for_device(target->srp_host->dev->dev->dma_device, iu->dma,
Vu Pham74b0a152006-06-17 20:37:32 -07001015 srp_max_iu_len, DMA_TO_DEVICE);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001016
1017 if (__srp_post_send(target, iu, len)) {
1018 printk(KERN_ERR PFX "Send failed\n");
1019 goto err_unmap;
1020 }
1021
Roland Dreierd945e1d2006-05-09 10:50:28 -07001022 list_move_tail(&req->list, &target->req_queue);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001023
1024 return 0;
1025
1026err_unmap:
1027 srp_unmap_data(scmnd, target, req);
1028
1029err:
1030 return SCSI_MLQUEUE_HOST_BUSY;
1031}
1032
1033static int srp_alloc_iu_bufs(struct srp_target_port *target)
1034{
1035 int i;
1036
1037 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1038 target->rx_ring[i] = srp_alloc_iu(target->srp_host,
1039 target->max_ti_iu_len,
1040 GFP_KERNEL, DMA_FROM_DEVICE);
1041 if (!target->rx_ring[i])
1042 goto err;
1043 }
1044
1045 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1046 target->tx_ring[i] = srp_alloc_iu(target->srp_host,
Vu Pham74b0a152006-06-17 20:37:32 -07001047 srp_max_iu_len,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001048 GFP_KERNEL, DMA_TO_DEVICE);
1049 if (!target->tx_ring[i])
1050 goto err;
1051 }
1052
1053 return 0;
1054
1055err:
1056 for (i = 0; i < SRP_RQ_SIZE; ++i) {
1057 srp_free_iu(target->srp_host, target->rx_ring[i]);
1058 target->rx_ring[i] = NULL;
1059 }
1060
1061 for (i = 0; i < SRP_SQ_SIZE + 1; ++i) {
1062 srp_free_iu(target->srp_host, target->tx_ring[i]);
1063 target->tx_ring[i] = NULL;
1064 }
1065
1066 return -ENOMEM;
1067}
1068
1069static void srp_cm_rej_handler(struct ib_cm_id *cm_id,
1070 struct ib_cm_event *event,
1071 struct srp_target_port *target)
1072{
1073 struct ib_class_port_info *cpi;
1074 int opcode;
1075
1076 switch (event->param.rej_rcvd.reason) {
1077 case IB_CM_REJ_PORT_CM_REDIRECT:
1078 cpi = event->param.rej_rcvd.ari;
1079 target->path.dlid = cpi->redirect_lid;
1080 target->path.pkey = cpi->redirect_pkey;
1081 cm_id->remote_cm_qpn = be32_to_cpu(cpi->redirect_qp) & 0x00ffffff;
1082 memcpy(target->path.dgid.raw, cpi->redirect_gid, 16);
1083
1084 target->status = target->path.dlid ?
1085 SRP_DLID_REDIRECT : SRP_PORT_REDIRECT;
1086 break;
1087
1088 case IB_CM_REJ_PORT_REDIRECT:
1089 if (topspin_workarounds &&
1090 !memcmp(&target->ioc_guid, topspin_oui, 3)) {
1091 /*
1092 * Topspin/Cisco SRP gateways incorrectly send
1093 * reject reason code 25 when they mean 24
1094 * (port redirect).
1095 */
1096 memcpy(target->path.dgid.raw,
1097 event->param.rej_rcvd.ari, 16);
1098
1099 printk(KERN_DEBUG PFX "Topspin/Cisco redirect to target port GID %016llx%016llx\n",
1100 (unsigned long long) be64_to_cpu(target->path.dgid.global.subnet_prefix),
1101 (unsigned long long) be64_to_cpu(target->path.dgid.global.interface_id));
1102
1103 target->status = SRP_PORT_REDIRECT;
1104 } else {
1105 printk(KERN_WARNING " REJ reason: IB_CM_REJ_PORT_REDIRECT\n");
1106 target->status = -ECONNRESET;
1107 }
1108 break;
1109
1110 case IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID:
1111 printk(KERN_WARNING " REJ reason: IB_CM_REJ_DUPLICATE_LOCAL_COMM_ID\n");
1112 target->status = -ECONNRESET;
1113 break;
1114
1115 case IB_CM_REJ_CONSUMER_DEFINED:
1116 opcode = *(u8 *) event->private_data;
1117 if (opcode == SRP_LOGIN_REJ) {
1118 struct srp_login_rej *rej = event->private_data;
1119 u32 reason = be32_to_cpu(rej->reason);
1120
1121 if (reason == SRP_LOGIN_REJ_REQ_IT_IU_LENGTH_TOO_LARGE)
1122 printk(KERN_WARNING PFX
1123 "SRP_LOGIN_REJ: requested max_it_iu_len too large\n");
1124 else
1125 printk(KERN_WARNING PFX
1126 "SRP LOGIN REJECTED, reason 0x%08x\n", reason);
1127 } else
1128 printk(KERN_WARNING " REJ reason: IB_CM_REJ_CONSUMER_DEFINED,"
1129 " opcode 0x%02x\n", opcode);
1130 target->status = -ECONNRESET;
1131 break;
1132
1133 default:
1134 printk(KERN_WARNING " REJ reason 0x%x\n",
1135 event->param.rej_rcvd.reason);
1136 target->status = -ECONNRESET;
1137 }
1138}
1139
1140static int srp_cm_handler(struct ib_cm_id *cm_id, struct ib_cm_event *event)
1141{
1142 struct srp_target_port *target = cm_id->context;
1143 struct ib_qp_attr *qp_attr = NULL;
1144 int attr_mask = 0;
1145 int comp = 0;
1146 int opcode = 0;
1147
1148 switch (event->event) {
1149 case IB_CM_REQ_ERROR:
1150 printk(KERN_DEBUG PFX "Sending CM REQ failed\n");
1151 comp = 1;
1152 target->status = -ECONNRESET;
1153 break;
1154
1155 case IB_CM_REP_RECEIVED:
1156 comp = 1;
1157 opcode = *(u8 *) event->private_data;
1158
1159 if (opcode == SRP_LOGIN_RSP) {
1160 struct srp_login_rsp *rsp = event->private_data;
1161
1162 target->max_ti_iu_len = be32_to_cpu(rsp->max_ti_iu_len);
1163 target->req_lim = be32_to_cpu(rsp->req_lim_delta);
1164
1165 target->scsi_host->can_queue = min(target->req_lim,
1166 target->scsi_host->can_queue);
1167 } else {
1168 printk(KERN_WARNING PFX "Unhandled RSP opcode %#x\n", opcode);
1169 target->status = -ECONNRESET;
1170 break;
1171 }
1172
1173 target->status = srp_alloc_iu_bufs(target);
1174 if (target->status)
1175 break;
1176
1177 qp_attr = kmalloc(sizeof *qp_attr, GFP_KERNEL);
1178 if (!qp_attr) {
1179 target->status = -ENOMEM;
1180 break;
1181 }
1182
1183 qp_attr->qp_state = IB_QPS_RTR;
1184 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1185 if (target->status)
1186 break;
1187
1188 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1189 if (target->status)
1190 break;
1191
1192 target->status = srp_post_recv(target);
1193 if (target->status)
1194 break;
1195
1196 qp_attr->qp_state = IB_QPS_RTS;
1197 target->status = ib_cm_init_qp_attr(cm_id, qp_attr, &attr_mask);
1198 if (target->status)
1199 break;
1200
1201 target->status = ib_modify_qp(target->qp, qp_attr, attr_mask);
1202 if (target->status)
1203 break;
1204
1205 target->status = ib_send_cm_rtu(cm_id, NULL, 0);
1206 if (target->status)
1207 break;
1208
1209 break;
1210
1211 case IB_CM_REJ_RECEIVED:
1212 printk(KERN_DEBUG PFX "REJ received\n");
1213 comp = 1;
1214
1215 srp_cm_rej_handler(cm_id, event, target);
1216 break;
1217
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001218 case IB_CM_DREQ_RECEIVED:
1219 printk(KERN_WARNING PFX "DREQ received - connection closed\n");
1220 if (ib_send_cm_drep(cm_id, NULL, 0))
1221 printk(KERN_ERR PFX "Sending CM DREP failed\n");
Roland Dreieraef9ec32005-11-02 14:07:13 -08001222 break;
1223
1224 case IB_CM_TIMEWAIT_EXIT:
1225 printk(KERN_ERR PFX "connection closed\n");
1226
1227 comp = 1;
1228 target->status = 0;
1229 break;
1230
Ishai Rabinovitzb7ac4ab2006-06-17 20:37:32 -07001231 case IB_CM_MRA_RECEIVED:
1232 case IB_CM_DREQ_ERROR:
1233 case IB_CM_DREP_RECEIVED:
1234 break;
1235
Roland Dreieraef9ec32005-11-02 14:07:13 -08001236 default:
1237 printk(KERN_WARNING PFX "Unhandled CM event %d\n", event->event);
1238 break;
1239 }
1240
1241 if (comp)
1242 complete(&target->done);
1243
1244 kfree(qp_attr);
1245
1246 return 0;
1247}
1248
Roland Dreierd945e1d2006-05-09 10:50:28 -07001249static int srp_send_tsk_mgmt(struct srp_target_port *target,
1250 struct srp_request *req, u8 func)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001251{
Roland Dreieraef9ec32005-11-02 14:07:13 -08001252 struct srp_iu *iu;
1253 struct srp_tsk_mgmt *tsk_mgmt;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001254
1255 spin_lock_irq(target->scsi_host->host_lock);
1256
Roland Dreier1285b3a2006-03-03 15:47:25 -08001257 if (target->state == SRP_TARGET_DEAD ||
1258 target->state == SRP_TARGET_REMOVED) {
Roland Dreierd945e1d2006-05-09 10:50:28 -07001259 req->scmnd->result = DID_BAD_TARGET << 16;
Roland Dreier1285b3a2006-03-03 15:47:25 -08001260 goto out;
1261 }
1262
Roland Dreieraef9ec32005-11-02 14:07:13 -08001263 init_completion(&req->done);
1264
1265 iu = __srp_get_tx_iu(target);
1266 if (!iu)
1267 goto out;
1268
1269 tsk_mgmt = iu->buf;
1270 memset(tsk_mgmt, 0, sizeof *tsk_mgmt);
1271
1272 tsk_mgmt->opcode = SRP_TSK_MGMT;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001273 tsk_mgmt->lun = cpu_to_be64((u64) req->scmnd->device->lun << 48);
1274 tsk_mgmt->tag = req->index | SRP_TAG_TSK_MGMT;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001275 tsk_mgmt->tsk_mgmt_func = func;
Roland Dreierd945e1d2006-05-09 10:50:28 -07001276 tsk_mgmt->task_tag = req->index;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001277
1278 if (__srp_post_send(target, iu, sizeof *tsk_mgmt))
1279 goto out;
1280
1281 req->tsk_mgmt = iu;
1282
1283 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001284
Roland Dreieraef9ec32005-11-02 14:07:13 -08001285 if (!wait_for_completion_timeout(&req->done,
1286 msecs_to_jiffies(SRP_ABORT_TIMEOUT_MS)))
Roland Dreierd945e1d2006-05-09 10:50:28 -07001287 return -1;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001288
Roland Dreierd945e1d2006-05-09 10:50:28 -07001289 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001290
1291out:
1292 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001293 return -1;
1294}
1295
1296static int srp_find_req(struct srp_target_port *target,
1297 struct scsi_cmnd *scmnd,
1298 struct srp_request **req)
1299{
1300 if (scmnd->host_scribble == (void *) -1L)
1301 return -1;
1302
1303 *req = &target->req_ring[(long) scmnd->host_scribble];
1304
1305 return 0;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001306}
1307
1308static int srp_abort(struct scsi_cmnd *scmnd)
1309{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001310 struct srp_target_port *target = host_to_target(scmnd->device->host);
1311 struct srp_request *req;
1312 int ret = SUCCESS;
1313
Roland Dreieraef9ec32005-11-02 14:07:13 -08001314 printk(KERN_ERR "SRP abort called\n");
1315
Roland Dreierd945e1d2006-05-09 10:50:28 -07001316 if (srp_find_req(target, scmnd, &req))
1317 return FAILED;
1318 if (srp_send_tsk_mgmt(target, req, SRP_TSK_ABORT_TASK))
1319 return FAILED;
1320
1321 spin_lock_irq(target->scsi_host->host_lock);
1322
1323 if (req->cmd_done) {
1324 srp_remove_req(target, req);
1325 scmnd->scsi_done(scmnd);
1326 } else if (!req->tsk_status) {
1327 srp_remove_req(target, req);
1328 scmnd->result = DID_ABORT << 16;
1329 } else
1330 ret = FAILED;
1331
1332 spin_unlock_irq(target->scsi_host->host_lock);
1333
1334 return ret;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001335}
1336
1337static int srp_reset_device(struct scsi_cmnd *scmnd)
1338{
Roland Dreierd945e1d2006-05-09 10:50:28 -07001339 struct srp_target_port *target = host_to_target(scmnd->device->host);
1340 struct srp_request *req, *tmp;
1341
Roland Dreieraef9ec32005-11-02 14:07:13 -08001342 printk(KERN_ERR "SRP reset_device called\n");
1343
Roland Dreierd945e1d2006-05-09 10:50:28 -07001344 if (srp_find_req(target, scmnd, &req))
1345 return FAILED;
1346 if (srp_send_tsk_mgmt(target, req, SRP_TSK_LUN_RESET))
1347 return FAILED;
1348 if (req->tsk_status)
1349 return FAILED;
1350
1351 spin_lock_irq(target->scsi_host->host_lock);
1352
1353 list_for_each_entry_safe(req, tmp, &target->req_queue, list)
Ishai Rabinovitz526b4ca2006-06-17 20:37:38 -07001354 if (req->scmnd->device == scmnd->device)
1355 srp_reset_req(target, req);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001356
1357 spin_unlock_irq(target->scsi_host->host_lock);
1358
1359 return SUCCESS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001360}
1361
1362static int srp_reset_host(struct scsi_cmnd *scmnd)
1363{
1364 struct srp_target_port *target = host_to_target(scmnd->device->host);
1365 int ret = FAILED;
1366
1367 printk(KERN_ERR PFX "SRP reset_host called\n");
1368
1369 if (!srp_reconnect_target(target))
1370 ret = SUCCESS;
1371
1372 return ret;
1373}
1374
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001375static ssize_t show_id_ext(struct class_device *cdev, char *buf)
1376{
1377 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1378
1379 if (target->state == SRP_TARGET_DEAD ||
1380 target->state == SRP_TARGET_REMOVED)
1381 return -ENODEV;
1382
1383 return sprintf(buf, "0x%016llx\n",
1384 (unsigned long long) be64_to_cpu(target->id_ext));
1385}
1386
1387static ssize_t show_ioc_guid(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->ioc_guid));
1397}
1398
1399static ssize_t show_service_id(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->service_id));
1409}
1410
1411static ssize_t show_pkey(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%04x\n", be16_to_cpu(target->path.pkey));
1420}
1421
1422static ssize_t show_dgid(struct class_device *cdev, char *buf)
1423{
1424 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1425
1426 if (target->state == SRP_TARGET_DEAD ||
1427 target->state == SRP_TARGET_REMOVED)
1428 return -ENODEV;
1429
1430 return sprintf(buf, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1431 be16_to_cpu(((__be16 *) target->path.dgid.raw)[0]),
1432 be16_to_cpu(((__be16 *) target->path.dgid.raw)[1]),
1433 be16_to_cpu(((__be16 *) target->path.dgid.raw)[2]),
1434 be16_to_cpu(((__be16 *) target->path.dgid.raw)[3]),
1435 be16_to_cpu(((__be16 *) target->path.dgid.raw)[4]),
1436 be16_to_cpu(((__be16 *) target->path.dgid.raw)[5]),
1437 be16_to_cpu(((__be16 *) target->path.dgid.raw)[6]),
1438 be16_to_cpu(((__be16 *) target->path.dgid.raw)[7]));
1439}
1440
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001441static ssize_t show_zero_req_lim(struct class_device *cdev, char *buf)
1442{
1443 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1444
1445 if (target->state == SRP_TARGET_DEAD ||
1446 target->state == SRP_TARGET_REMOVED)
1447 return -ENODEV;
1448
1449 return sprintf(buf, "%d\n", target->zero_req_lim);
1450}
1451
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001452static ssize_t show_local_ib_port(struct class_device *cdev, char *buf)
1453{
1454 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1455
1456 return sprintf(buf, "%d\n", target->srp_host->port);
1457}
1458
1459static ssize_t show_local_ib_device(struct class_device *cdev, char *buf)
1460{
1461 struct srp_target_port *target = host_to_target(class_to_shost(cdev));
1462
1463 return sprintf(buf, "%s\n", target->srp_host->dev->dev->name);
1464}
1465
1466static CLASS_DEVICE_ATTR(id_ext, S_IRUGO, show_id_ext, NULL);
1467static CLASS_DEVICE_ATTR(ioc_guid, S_IRUGO, show_ioc_guid, NULL);
1468static CLASS_DEVICE_ATTR(service_id, S_IRUGO, show_service_id, NULL);
1469static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1470static CLASS_DEVICE_ATTR(dgid, S_IRUGO, show_dgid, NULL);
1471static CLASS_DEVICE_ATTR(zero_req_lim, S_IRUGO, show_zero_req_lim, NULL);
1472static CLASS_DEVICE_ATTR(local_ib_port, S_IRUGO, show_local_ib_port, NULL);
1473static CLASS_DEVICE_ATTR(local_ib_device, S_IRUGO, show_local_ib_device, NULL);
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001474
1475static struct class_device_attribute *srp_host_attrs[] = {
1476 &class_device_attr_id_ext,
1477 &class_device_attr_ioc_guid,
1478 &class_device_attr_service_id,
1479 &class_device_attr_pkey,
1480 &class_device_attr_dgid,
Roland Dreier6bfa24f2006-06-17 20:37:33 -07001481 &class_device_attr_zero_req_lim,
Ishai Rabinovitzded7f1a2006-08-15 17:34:52 +03001482 &class_device_attr_local_ib_port,
1483 &class_device_attr_local_ib_device,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001484 NULL
1485};
1486
Roland Dreieraef9ec32005-11-02 14:07:13 -08001487static struct scsi_host_template srp_template = {
1488 .module = THIS_MODULE,
1489 .name = DRV_NAME,
1490 .info = srp_target_info,
1491 .queuecommand = srp_queuecommand,
1492 .eh_abort_handler = srp_abort,
1493 .eh_device_reset_handler = srp_reset_device,
1494 .eh_host_reset_handler = srp_reset_host,
1495 .can_queue = SRP_SQ_SIZE,
1496 .this_id = -1,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001497 .cmd_per_lun = SRP_SQ_SIZE,
Roland Dreier6ecb0c82006-03-20 10:08:23 -08001498 .use_clustering = ENABLE_CLUSTERING,
1499 .shost_attrs = srp_host_attrs
Roland Dreieraef9ec32005-11-02 14:07:13 -08001500};
1501
1502static int srp_add_target(struct srp_host *host, struct srp_target_port *target)
1503{
1504 sprintf(target->target_name, "SRP.T10:%016llX",
1505 (unsigned long long) be64_to_cpu(target->id_ext));
1506
Roland Dreierf5358a12006-06-17 20:37:29 -07001507 if (scsi_add_host(target->scsi_host, host->dev->dev->dma_device))
Roland Dreieraef9ec32005-11-02 14:07:13 -08001508 return -ENODEV;
1509
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001510 spin_lock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001511 list_add_tail(&target->list, &host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001512 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001513
1514 target->state = SRP_TARGET_LIVE;
1515
Roland Dreieraef9ec32005-11-02 14:07:13 -08001516 scsi_scan_target(&target->scsi_host->shost_gendev,
Matthew Wilcox1962a4a2006-06-17 20:37:30 -07001517 0, target->scsi_id, SCAN_WILD_CARD, 0);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001518
1519 return 0;
1520}
1521
1522static void srp_release_class_dev(struct class_device *class_dev)
1523{
1524 struct srp_host *host =
1525 container_of(class_dev, struct srp_host, class_dev);
1526
1527 complete(&host->released);
1528}
1529
1530static struct class srp_class = {
1531 .name = "infiniband_srp",
1532 .release = srp_release_class_dev
1533};
1534
1535/*
1536 * Target ports are added by writing
1537 *
1538 * id_ext=<SRP ID ext>,ioc_guid=<SRP IOC GUID>,dgid=<dest GID>,
1539 * pkey=<P_Key>,service_id=<service ID>
1540 *
1541 * to the add_target sysfs attribute.
1542 */
1543enum {
1544 SRP_OPT_ERR = 0,
1545 SRP_OPT_ID_EXT = 1 << 0,
1546 SRP_OPT_IOC_GUID = 1 << 1,
1547 SRP_OPT_DGID = 1 << 2,
1548 SRP_OPT_PKEY = 1 << 3,
1549 SRP_OPT_SERVICE_ID = 1 << 4,
1550 SRP_OPT_MAX_SECT = 1 << 5,
Vu Pham52fb2b502006-06-17 20:37:31 -07001551 SRP_OPT_MAX_CMD_PER_LUN = 1 << 6,
Ramachandra K0c0450db2006-06-17 20:37:38 -07001552 SRP_OPT_IO_CLASS = 1 << 7,
Roland Dreieraef9ec32005-11-02 14:07:13 -08001553 SRP_OPT_ALL = (SRP_OPT_ID_EXT |
1554 SRP_OPT_IOC_GUID |
1555 SRP_OPT_DGID |
1556 SRP_OPT_PKEY |
1557 SRP_OPT_SERVICE_ID),
1558};
1559
1560static match_table_t srp_opt_tokens = {
Vu Pham52fb2b502006-06-17 20:37:31 -07001561 { SRP_OPT_ID_EXT, "id_ext=%s" },
1562 { SRP_OPT_IOC_GUID, "ioc_guid=%s" },
1563 { SRP_OPT_DGID, "dgid=%s" },
1564 { SRP_OPT_PKEY, "pkey=%x" },
1565 { SRP_OPT_SERVICE_ID, "service_id=%s" },
1566 { SRP_OPT_MAX_SECT, "max_sect=%d" },
1567 { SRP_OPT_MAX_CMD_PER_LUN, "max_cmd_per_lun=%d" },
Ramachandra K0c0450db2006-06-17 20:37:38 -07001568 { SRP_OPT_IO_CLASS, "io_class=%x" },
Vu Pham52fb2b502006-06-17 20:37:31 -07001569 { SRP_OPT_ERR, NULL }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001570};
1571
1572static int srp_parse_options(const char *buf, struct srp_target_port *target)
1573{
1574 char *options, *sep_opt;
1575 char *p;
1576 char dgid[3];
1577 substring_t args[MAX_OPT_ARGS];
1578 int opt_mask = 0;
1579 int token;
1580 int ret = -EINVAL;
1581 int i;
1582
1583 options = kstrdup(buf, GFP_KERNEL);
1584 if (!options)
1585 return -ENOMEM;
1586
1587 sep_opt = options;
1588 while ((p = strsep(&sep_opt, ",")) != NULL) {
1589 if (!*p)
1590 continue;
1591
1592 token = match_token(p, srp_opt_tokens, args);
1593 opt_mask |= token;
1594
1595 switch (token) {
1596 case SRP_OPT_ID_EXT:
1597 p = match_strdup(args);
1598 target->id_ext = cpu_to_be64(simple_strtoull(p, NULL, 16));
1599 kfree(p);
1600 break;
1601
1602 case SRP_OPT_IOC_GUID:
1603 p = match_strdup(args);
1604 target->ioc_guid = cpu_to_be64(simple_strtoull(p, NULL, 16));
1605 kfree(p);
1606 break;
1607
1608 case SRP_OPT_DGID:
1609 p = match_strdup(args);
1610 if (strlen(p) != 32) {
1611 printk(KERN_WARNING PFX "bad dest GID parameter '%s'\n", p);
Roland Dreierce1823f2006-04-03 09:31:04 -07001612 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001613 goto out;
1614 }
1615
1616 for (i = 0; i < 16; ++i) {
1617 strlcpy(dgid, p + i * 2, 3);
1618 target->path.dgid.raw[i] = simple_strtoul(dgid, NULL, 16);
1619 }
Roland Dreierbf17c1c2006-03-20 10:08:25 -08001620 kfree(p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001621 break;
1622
1623 case SRP_OPT_PKEY:
1624 if (match_hex(args, &token)) {
1625 printk(KERN_WARNING PFX "bad P_Key parameter '%s'\n", p);
1626 goto out;
1627 }
1628 target->path.pkey = cpu_to_be16(token);
1629 break;
1630
1631 case SRP_OPT_SERVICE_ID:
1632 p = match_strdup(args);
1633 target->service_id = cpu_to_be64(simple_strtoull(p, NULL, 16));
1634 kfree(p);
1635 break;
1636
1637 case SRP_OPT_MAX_SECT:
1638 if (match_int(args, &token)) {
1639 printk(KERN_WARNING PFX "bad max sect parameter '%s'\n", p);
1640 goto out;
1641 }
1642 target->scsi_host->max_sectors = token;
1643 break;
1644
Vu Pham52fb2b502006-06-17 20:37:31 -07001645 case SRP_OPT_MAX_CMD_PER_LUN:
1646 if (match_int(args, &token)) {
1647 printk(KERN_WARNING PFX "bad max cmd_per_lun parameter '%s'\n", p);
1648 goto out;
1649 }
1650 target->scsi_host->cmd_per_lun = min(token, SRP_SQ_SIZE);
1651 break;
1652
Ramachandra K0c0450db2006-06-17 20:37:38 -07001653 case SRP_OPT_IO_CLASS:
1654 if (match_hex(args, &token)) {
1655 printk(KERN_WARNING PFX "bad IO class parameter '%s' \n", p);
1656 goto out;
1657 }
1658 if (token != SRP_REV10_IB_IO_CLASS &&
1659 token != SRP_REV16A_IB_IO_CLASS) {
1660 printk(KERN_WARNING PFX "unknown IO class parameter value"
1661 " %x specified (use %x or %x).\n",
1662 token, SRP_REV10_IB_IO_CLASS, SRP_REV16A_IB_IO_CLASS);
1663 goto out;
1664 }
1665 target->io_class = token;
1666 break;
1667
Roland Dreieraef9ec32005-11-02 14:07:13 -08001668 default:
1669 printk(KERN_WARNING PFX "unknown parameter or missing value "
1670 "'%s' in target creation request\n", p);
1671 goto out;
1672 }
1673 }
1674
1675 if ((opt_mask & SRP_OPT_ALL) == SRP_OPT_ALL)
1676 ret = 0;
1677 else
1678 for (i = 0; i < ARRAY_SIZE(srp_opt_tokens); ++i)
1679 if ((srp_opt_tokens[i].token & SRP_OPT_ALL) &&
1680 !(srp_opt_tokens[i].token & opt_mask))
1681 printk(KERN_WARNING PFX "target creation request is "
1682 "missing parameter '%s'\n",
1683 srp_opt_tokens[i].pattern);
1684
1685out:
1686 kfree(options);
1687 return ret;
1688}
1689
1690static ssize_t srp_create_target(struct class_device *class_dev,
1691 const char *buf, size_t count)
1692{
1693 struct srp_host *host =
1694 container_of(class_dev, struct srp_host, class_dev);
1695 struct Scsi_Host *target_host;
1696 struct srp_target_port *target;
1697 int ret;
1698 int i;
1699
1700 target_host = scsi_host_alloc(&srp_template,
1701 sizeof (struct srp_target_port));
1702 if (!target_host)
1703 return -ENOMEM;
1704
Roland Dreier5f068992005-11-11 14:06:01 -08001705 target_host->max_lun = SRP_MAX_LUN;
1706
Roland Dreieraef9ec32005-11-02 14:07:13 -08001707 target = host_to_target(target_host);
1708 memset(target, 0, sizeof *target);
1709
Ramachandra K0c0450db2006-06-17 20:37:38 -07001710 target->io_class = SRP_REV16A_IB_IO_CLASS;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001711 target->scsi_host = target_host;
1712 target->srp_host = host;
1713
Roland Dreierd945e1d2006-05-09 10:50:28 -07001714 INIT_LIST_HEAD(&target->free_reqs);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001715 INIT_LIST_HEAD(&target->req_queue);
Roland Dreierd945e1d2006-05-09 10:50:28 -07001716 for (i = 0; i < SRP_SQ_SIZE; ++i) {
1717 target->req_ring[i].index = i;
1718 list_add_tail(&target->req_ring[i].list, &target->free_reqs);
1719 }
Roland Dreieraef9ec32005-11-02 14:07:13 -08001720
1721 ret = srp_parse_options(buf, target);
1722 if (ret)
1723 goto err;
1724
Roland Dreierf5358a12006-06-17 20:37:29 -07001725 ib_get_cached_gid(host->dev->dev, host->port, 0, &target->path.sgid);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001726
1727 printk(KERN_DEBUG PFX "new target: id_ext %016llx ioc_guid %016llx pkey %04x "
1728 "service_id %016llx dgid %04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x\n",
1729 (unsigned long long) be64_to_cpu(target->id_ext),
1730 (unsigned long long) be64_to_cpu(target->ioc_guid),
1731 be16_to_cpu(target->path.pkey),
1732 (unsigned long long) be64_to_cpu(target->service_id),
1733 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[0]),
1734 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[2]),
1735 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[4]),
1736 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[6]),
1737 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[8]),
1738 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[10]),
1739 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[12]),
1740 (int) be16_to_cpu(*(__be16 *) &target->path.dgid.raw[14]));
1741
1742 ret = srp_create_target_ib(target);
1743 if (ret)
1744 goto err;
1745
Roland Dreierf5358a12006-06-17 20:37:29 -07001746 target->cm_id = ib_create_cm_id(host->dev->dev, srp_cm_handler, target);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001747 if (IS_ERR(target->cm_id)) {
1748 ret = PTR_ERR(target->cm_id);
1749 goto err_free;
1750 }
1751
1752 ret = srp_connect_target(target);
1753 if (ret) {
1754 printk(KERN_ERR PFX "Connection failed\n");
1755 goto err_cm_id;
1756 }
1757
1758 ret = srp_add_target(host, target);
1759 if (ret)
1760 goto err_disconnect;
1761
1762 return count;
1763
1764err_disconnect:
1765 srp_disconnect_target(target);
1766
1767err_cm_id:
1768 ib_destroy_cm_id(target->cm_id);
1769
1770err_free:
1771 srp_free_target_ib(target);
1772
1773err:
1774 scsi_host_put(target_host);
1775
1776 return ret;
1777}
1778
1779static CLASS_DEVICE_ATTR(add_target, S_IWUSR, NULL, srp_create_target);
1780
1781static ssize_t show_ibdev(struct class_device *class_dev, char *buf)
1782{
1783 struct srp_host *host =
1784 container_of(class_dev, struct srp_host, class_dev);
1785
Roland Dreierf5358a12006-06-17 20:37:29 -07001786 return sprintf(buf, "%s\n", host->dev->dev->name);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001787}
1788
1789static CLASS_DEVICE_ATTR(ibdev, S_IRUGO, show_ibdev, NULL);
1790
1791static ssize_t show_port(struct class_device *class_dev, char *buf)
1792{
1793 struct srp_host *host =
1794 container_of(class_dev, struct srp_host, class_dev);
1795
1796 return sprintf(buf, "%d\n", host->port);
1797}
1798
1799static CLASS_DEVICE_ATTR(port, S_IRUGO, show_port, NULL);
1800
Roland Dreierf5358a12006-06-17 20:37:29 -07001801static struct srp_host *srp_add_port(struct srp_device *device, u8 port)
Roland Dreieraef9ec32005-11-02 14:07:13 -08001802{
1803 struct srp_host *host;
1804
1805 host = kzalloc(sizeof *host, GFP_KERNEL);
1806 if (!host)
1807 return NULL;
1808
1809 INIT_LIST_HEAD(&host->target_list);
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001810 spin_lock_init(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001811 init_completion(&host->released);
1812 host->dev = device;
1813 host->port = port;
1814
1815 host->initiator_port_id[7] = port;
Roland Dreierf5358a12006-06-17 20:37:29 -07001816 memcpy(host->initiator_port_id + 8, &device->dev->node_guid, 8);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001817
1818 host->class_dev.class = &srp_class;
Roland Dreierf5358a12006-06-17 20:37:29 -07001819 host->class_dev.dev = device->dev->dma_device;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001820 snprintf(host->class_dev.class_id, BUS_ID_SIZE, "srp-%s-%d",
Roland Dreierf5358a12006-06-17 20:37:29 -07001821 device->dev->name, port);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001822
1823 if (class_device_register(&host->class_dev))
Roland Dreierf5358a12006-06-17 20:37:29 -07001824 goto free_host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001825 if (class_device_create_file(&host->class_dev, &class_device_attr_add_target))
1826 goto err_class;
1827 if (class_device_create_file(&host->class_dev, &class_device_attr_ibdev))
1828 goto err_class;
1829 if (class_device_create_file(&host->class_dev, &class_device_attr_port))
1830 goto err_class;
1831
1832 return host;
1833
1834err_class:
1835 class_device_unregister(&host->class_dev);
1836
Roland Dreierf5358a12006-06-17 20:37:29 -07001837free_host:
Roland Dreieraef9ec32005-11-02 14:07:13 -08001838 kfree(host);
1839
1840 return NULL;
1841}
1842
1843static void srp_add_one(struct ib_device *device)
1844{
Roland Dreierf5358a12006-06-17 20:37:29 -07001845 struct srp_device *srp_dev;
1846 struct ib_device_attr *dev_attr;
1847 struct ib_fmr_pool_param fmr_param;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001848 struct srp_host *host;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001849 int s, e, p;
1850
Roland Dreierf5358a12006-06-17 20:37:29 -07001851 dev_attr = kmalloc(sizeof *dev_attr, GFP_KERNEL);
1852 if (!dev_attr)
Sean Heftycf311cd2006-01-10 07:39:34 -08001853 return;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001854
Roland Dreierf5358a12006-06-17 20:37:29 -07001855 if (ib_query_device(device, dev_attr)) {
1856 printk(KERN_WARNING PFX "Query device failed for %s\n",
1857 device->name);
1858 goto free_attr;
1859 }
1860
1861 srp_dev = kmalloc(sizeof *srp_dev, GFP_KERNEL);
1862 if (!srp_dev)
1863 goto free_attr;
1864
1865 /*
1866 * Use the smallest page size supported by the HCA, down to a
1867 * minimum of 512 bytes (which is the smallest sector that a
1868 * SCSI command will ever carry).
1869 */
1870 srp_dev->fmr_page_shift = max(9, ffs(dev_attr->page_size_cap) - 1);
1871 srp_dev->fmr_page_size = 1 << srp_dev->fmr_page_shift;
1872 srp_dev->fmr_page_mask = ~((unsigned long) srp_dev->fmr_page_size - 1);
1873
1874 INIT_LIST_HEAD(&srp_dev->dev_list);
1875
1876 srp_dev->dev = device;
1877 srp_dev->pd = ib_alloc_pd(device);
1878 if (IS_ERR(srp_dev->pd))
1879 goto free_dev;
1880
1881 srp_dev->mr = ib_get_dma_mr(srp_dev->pd,
1882 IB_ACCESS_LOCAL_WRITE |
1883 IB_ACCESS_REMOTE_READ |
1884 IB_ACCESS_REMOTE_WRITE);
1885 if (IS_ERR(srp_dev->mr))
1886 goto err_pd;
1887
1888 memset(&fmr_param, 0, sizeof fmr_param);
1889 fmr_param.pool_size = SRP_FMR_POOL_SIZE;
1890 fmr_param.dirty_watermark = SRP_FMR_DIRTY_SIZE;
1891 fmr_param.cache = 1;
1892 fmr_param.max_pages_per_fmr = SRP_FMR_SIZE;
1893 fmr_param.page_shift = srp_dev->fmr_page_shift;
1894 fmr_param.access = (IB_ACCESS_LOCAL_WRITE |
1895 IB_ACCESS_REMOTE_WRITE |
1896 IB_ACCESS_REMOTE_READ);
1897
1898 srp_dev->fmr_pool = ib_create_fmr_pool(srp_dev->pd, &fmr_param);
1899 if (IS_ERR(srp_dev->fmr_pool))
1900 srp_dev->fmr_pool = NULL;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001901
1902 if (device->node_type == IB_NODE_SWITCH) {
1903 s = 0;
1904 e = 0;
1905 } else {
1906 s = 1;
1907 e = device->phys_port_cnt;
1908 }
1909
1910 for (p = s; p <= e; ++p) {
Roland Dreierf5358a12006-06-17 20:37:29 -07001911 host = srp_add_port(srp_dev, p);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001912 if (host)
Roland Dreierf5358a12006-06-17 20:37:29 -07001913 list_add_tail(&host->list, &srp_dev->dev_list);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001914 }
1915
Roland Dreierf5358a12006-06-17 20:37:29 -07001916 ib_set_client_data(device, &srp_client, srp_dev);
1917
1918 goto free_attr;
1919
1920err_pd:
1921 ib_dealloc_pd(srp_dev->pd);
1922
1923free_dev:
1924 kfree(srp_dev);
1925
1926free_attr:
1927 kfree(dev_attr);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001928}
1929
1930static void srp_remove_one(struct ib_device *device)
1931{
Roland Dreierf5358a12006-06-17 20:37:29 -07001932 struct srp_device *srp_dev;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001933 struct srp_host *host, *tmp_host;
1934 LIST_HEAD(target_list);
1935 struct srp_target_port *target, *tmp_target;
Roland Dreieraef9ec32005-11-02 14:07:13 -08001936
Roland Dreierf5358a12006-06-17 20:37:29 -07001937 srp_dev = ib_get_client_data(device, &srp_client);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001938
Roland Dreierf5358a12006-06-17 20:37:29 -07001939 list_for_each_entry_safe(host, tmp_host, &srp_dev->dev_list, list) {
Roland Dreieraef9ec32005-11-02 14:07:13 -08001940 class_device_unregister(&host->class_dev);
1941 /*
1942 * Wait for the sysfs entry to go away, so that no new
1943 * target ports can be created.
1944 */
1945 wait_for_completion(&host->released);
1946
1947 /*
1948 * Mark all target ports as removed, so we stop queueing
1949 * commands and don't try to reconnect.
1950 */
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001951 spin_lock(&host->target_lock);
Matthew Wilcox549c5fc22006-06-17 20:37:30 -07001952 list_for_each_entry(target, &host->target_list, list) {
Ishai Rabinovitz0c5b3952006-06-17 20:37:31 -07001953 spin_lock_irq(target->scsi_host->host_lock);
1954 target->state = SRP_TARGET_REMOVED;
1955 spin_unlock_irq(target->scsi_host->host_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001956 }
Matthew Wilcoxb3589fd2006-06-17 20:37:30 -07001957 spin_unlock(&host->target_lock);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001958
1959 /*
1960 * Wait for any reconnection tasks that may have
1961 * started before we marked our target ports as
1962 * removed, and any target port removal tasks.
1963 */
1964 flush_scheduled_work();
1965
1966 list_for_each_entry_safe(target, tmp_target,
1967 &host->target_list, list) {
1968 scsi_remove_host(target->scsi_host);
1969 srp_disconnect_target(target);
1970 ib_destroy_cm_id(target->cm_id);
1971 srp_free_target_ib(target);
1972 scsi_host_put(target->scsi_host);
1973 }
1974
Roland Dreieraef9ec32005-11-02 14:07:13 -08001975 kfree(host);
1976 }
1977
Roland Dreierf5358a12006-06-17 20:37:29 -07001978 if (srp_dev->fmr_pool)
1979 ib_destroy_fmr_pool(srp_dev->fmr_pool);
1980 ib_dereg_mr(srp_dev->mr);
1981 ib_dealloc_pd(srp_dev->pd);
1982
1983 kfree(srp_dev);
Roland Dreieraef9ec32005-11-02 14:07:13 -08001984}
1985
1986static int __init srp_init_module(void)
1987{
1988 int ret;
1989
Vu Pham74b0a152006-06-17 20:37:32 -07001990 srp_template.sg_tablesize = srp_sg_tablesize;
1991 srp_max_iu_len = (sizeof (struct srp_cmd) +
1992 sizeof (struct srp_indirect_buf) +
1993 srp_sg_tablesize * 16);
1994
Roland Dreieraef9ec32005-11-02 14:07:13 -08001995 ret = class_register(&srp_class);
1996 if (ret) {
1997 printk(KERN_ERR PFX "couldn't register class infiniband_srp\n");
1998 return ret;
1999 }
2000
2001 ret = ib_register_client(&srp_client);
2002 if (ret) {
2003 printk(KERN_ERR PFX "couldn't register IB client\n");
2004 class_unregister(&srp_class);
2005 return ret;
2006 }
2007
2008 return 0;
2009}
2010
2011static void __exit srp_cleanup_module(void)
2012{
2013 ib_unregister_client(&srp_client);
2014 class_unregister(&srp_class);
2015}
2016
2017module_init(srp_init_module);
2018module_exit(srp_cleanup_module);