Blame view

drivers/target/sbp/sbp_target.c 59.8 KB
a511ce339   Chris Boot   sbp-target: Initi...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
  /*
   * SBP2 target driver (SCSI over IEEE1394 in target mode)
   *
   * Copyright (C) 2011  Chris Boot <bootc@bootc.net>
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation; either version 2 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program; if not, write to the Free Software Foundation,
   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
   */
  
  #define KMSG_COMPONENT "sbp_target"
  #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  
  #include <linux/kernel.h>
  #include <linux/module.h>
  #include <linux/init.h>
  #include <linux/types.h>
  #include <linux/string.h>
  #include <linux/configfs.h>
  #include <linux/ctype.h>
4323e65b7   Bart Van Assche   sbp-target: Add a...
31
  #include <linux/delay.h>
a511ce339   Chris Boot   sbp-target: Initi...
32
33
  #include <linux/firewire.h>
  #include <linux/firewire-constants.h>
ba9299925   Bart Van Assche   target: Minimize ...
34
  #include <scsi/scsi_proto.h>
a511ce339   Chris Boot   sbp-target: Initi...
35
36
37
38
  #include <scsi/scsi_tcq.h>
  #include <target/target_core_base.h>
  #include <target/target_core_backend.h>
  #include <target/target_core_fabric.h>
a511ce339   Chris Boot   sbp-target: Initi...
39
40
41
  #include <asm/unaligned.h>
  
  #include "sbp_target.h"
a511ce339   Chris Boot   sbp-target: Initi...
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
  /* FireWire address region for management and command block address handlers */
  static const struct fw_address_region sbp_register_region = {
  	.start	= CSR_REGISTER_BASE + 0x10000,
  	.end	= 0x1000000000000ULL,
  };
  
  static const u32 sbp_unit_directory_template[] = {
  	0x1200609e, /* unit_specifier_id: NCITS/T10 */
  	0x13010483, /* unit_sw_version: 1155D Rev 4 */
  	0x3800609e, /* command_set_specifier_id: NCITS/T10 */
  	0x390104d8, /* command_set: SPC-2 */
  	0x3b000000, /* command_set_revision: 0 */
  	0x3c000001, /* firmware_revision: 1 */
  };
  
  #define SESSION_MAINTENANCE_INTERVAL HZ
  
  static atomic_t login_id = ATOMIC_INIT(0);
  
  static void session_maintenance_work(struct work_struct *);
  static int sbp_run_transaction(struct fw_card *, int, int, int, int,
  		unsigned long long, void *, size_t);
  
  static int read_peer_guid(u64 *guid, const struct sbp_management_request *req)
  {
  	int ret;
  	__be32 high, low;
  
  	ret = sbp_run_transaction(req->card, TCODE_READ_QUADLET_REQUEST,
  			req->node_addr, req->generation, req->speed,
  			(CSR_REGISTER_BASE | CSR_CONFIG_ROM) + 3 * 4,
  			&high, sizeof(high));
  	if (ret != RCODE_COMPLETE)
  		return ret;
  
  	ret = sbp_run_transaction(req->card, TCODE_READ_QUADLET_REQUEST,
  			req->node_addr, req->generation, req->speed,
  			(CSR_REGISTER_BASE | CSR_CONFIG_ROM) + 4 * 4,
  			&low, sizeof(low));
  	if (ret != RCODE_COMPLETE)
  		return ret;
  
  	*guid = (u64)be32_to_cpu(high) << 32 | be32_to_cpu(low);
  
  	return RCODE_COMPLETE;
  }
  
  static struct sbp_session *sbp_session_find_by_guid(
  	struct sbp_tpg *tpg, u64 guid)
  {
  	struct se_session *se_sess;
  	struct sbp_session *sess, *found = NULL;
  
  	spin_lock_bh(&tpg->se_tpg.session_lock);
  	list_for_each_entry(se_sess, &tpg->se_tpg.tpg_sess_list, sess_list) {
  		sess = se_sess->fabric_sess_ptr;
  		if (sess->guid == guid)
  			found = sess;
  	}
  	spin_unlock_bh(&tpg->se_tpg.session_lock);
  
  	return found;
  }
  
  static struct sbp_login_descriptor *sbp_login_find_by_lun(
6bb826121   Nicholas Bellinger   target: Convert s...
107
  		struct sbp_session *session, u32 unpacked_lun)
a511ce339   Chris Boot   sbp-target: Initi...
108
109
110
111
112
  {
  	struct sbp_login_descriptor *login, *found = NULL;
  
  	spin_lock_bh(&session->lock);
  	list_for_each_entry(login, &session->login_list, link) {
6bb826121   Nicholas Bellinger   target: Convert s...
113
  		if (login->login_lun == unpacked_lun)
a511ce339   Chris Boot   sbp-target: Initi...
114
115
116
117
118
119
120
121
122
  			found = login;
  	}
  	spin_unlock_bh(&session->lock);
  
  	return found;
  }
  
  static int sbp_login_count_all_by_lun(
  		struct sbp_tpg *tpg,
6bb826121   Nicholas Bellinger   target: Convert s...
123
  		u32 unpacked_lun,
a511ce339   Chris Boot   sbp-target: Initi...
124
125
126
127
128
129
130
131
132
133
134
135
136
  		int exclusive)
  {
  	struct se_session *se_sess;
  	struct sbp_session *sess;
  	struct sbp_login_descriptor *login;
  	int count = 0;
  
  	spin_lock_bh(&tpg->se_tpg.session_lock);
  	list_for_each_entry(se_sess, &tpg->se_tpg.tpg_sess_list, sess_list) {
  		sess = se_sess->fabric_sess_ptr;
  
  		spin_lock_bh(&sess->lock);
  		list_for_each_entry(login, &sess->login_list, link) {
6bb826121   Nicholas Bellinger   target: Convert s...
137
  			if (login->login_lun != unpacked_lun)
a511ce339   Chris Boot   sbp-target: Initi...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
  				continue;
  
  			if (!exclusive || login->exclusive)
  				count++;
  		}
  		spin_unlock_bh(&sess->lock);
  	}
  	spin_unlock_bh(&tpg->se_tpg.session_lock);
  
  	return count;
  }
  
  static struct sbp_login_descriptor *sbp_login_find_by_id(
  	struct sbp_tpg *tpg, int login_id)
  {
  	struct se_session *se_sess;
  	struct sbp_session *sess;
  	struct sbp_login_descriptor *login, *found = NULL;
  
  	spin_lock_bh(&tpg->se_tpg.session_lock);
  	list_for_each_entry(se_sess, &tpg->se_tpg.tpg_sess_list, sess_list) {
  		sess = se_sess->fabric_sess_ptr;
  
  		spin_lock_bh(&sess->lock);
  		list_for_each_entry(login, &sess->login_list, link) {
  			if (login->login_id == login_id)
  				found = login;
  		}
  		spin_unlock_bh(&sess->lock);
  	}
  	spin_unlock_bh(&tpg->se_tpg.session_lock);
  
  	return found;
  }
6bb826121   Nicholas Bellinger   target: Convert s...
172
  static u32 sbp_get_lun_from_tpg(struct sbp_tpg *tpg, u32 login_lun, int *err)
a511ce339   Chris Boot   sbp-target: Initi...
173
174
175
  {
  	struct se_portal_group *se_tpg = &tpg->se_tpg;
  	struct se_lun *se_lun;
6bb826121   Nicholas Bellinger   target: Convert s...
176
177
178
179
180
181
182
183
184
  	rcu_read_lock();
  	hlist_for_each_entry_rcu(se_lun, &se_tpg->tpg_lun_hlist, link) {
  		if (se_lun->unpacked_lun == login_lun) {
  			rcu_read_unlock();
  			*err = 0;
  			return login_lun;
  		}
  	}
  	rcu_read_unlock();
a511ce339   Chris Boot   sbp-target: Initi...
185

6bb826121   Nicholas Bellinger   target: Convert s...
186
187
  	*err = -ENODEV;
  	return login_lun;
a511ce339   Chris Boot   sbp-target: Initi...
188
189
190
191
192
193
194
195
196
  }
  
  static struct sbp_session *sbp_session_create(
  		struct sbp_tpg *tpg,
  		u64 guid)
  {
  	struct sbp_session *sess;
  	int ret;
  	char guid_str[17];
fb444abe6   Christoph Hellwig   target: Convert d...
197
198
  
  	snprintf(guid_str, sizeof(guid_str), "%016llx", guid);
a511ce339   Chris Boot   sbp-target: Initi...
199
200
  
  	sess = kmalloc(sizeof(*sess), GFP_KERNEL);
e0f3d4c23   Markus Elfring   sbp-target: Delet...
201
  	if (!sess)
a511ce339   Chris Boot   sbp-target: Initi...
202
  		return ERR_PTR(-ENOMEM);
e0f3d4c23   Markus Elfring   sbp-target: Delet...
203

fb444abe6   Christoph Hellwig   target: Convert d...
204
205
206
207
  	spin_lock_init(&sess->lock);
  	INIT_LIST_HEAD(&sess->login_list);
  	INIT_DELAYED_WORK(&sess->maint_work, session_maintenance_work);
  	sess->guid = guid;
a511ce339   Chris Boot   sbp-target: Initi...
208

fa8342873   Mike Christie   scsi: target: ren...
209
  	sess->se_sess = target_setup_session(&tpg->se_tpg, 128,
5a3ee221b   Nicholas Bellinger   sbp-target: Conve...
210
211
212
  					     sizeof(struct sbp_target_request),
  					     TARGET_PROT_NORMAL, guid_str,
  					     sess, NULL);
a511ce339   Chris Boot   sbp-target: Initi...
213
214
215
  	if (IS_ERR(sess->se_sess)) {
  		pr_err("failed to init se_session
  ");
a511ce339   Chris Boot   sbp-target: Initi...
216
217
218
219
  		ret = PTR_ERR(sess->se_sess);
  		kfree(sess);
  		return ERR_PTR(ret);
  	}
a511ce339   Chris Boot   sbp-target: Initi...
220
221
222
223
224
225
226
227
228
229
230
231
232
233
  	return sess;
  }
  
  static void sbp_session_release(struct sbp_session *sess, bool cancel_work)
  {
  	spin_lock_bh(&sess->lock);
  	if (!list_empty(&sess->login_list)) {
  		spin_unlock_bh(&sess->lock);
  		return;
  	}
  	spin_unlock_bh(&sess->lock);
  
  	if (cancel_work)
  		cancel_delayed_work_sync(&sess->maint_work);
b287e3517   Mike Christie   scsi: target: srp...
234
  	target_remove_session(sess->se_sess);
a511ce339   Chris Boot   sbp-target: Initi...
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
  
  	if (sess->card)
  		fw_card_put(sess->card);
  
  	kfree(sess);
  }
  
  static void sbp_target_agent_unregister(struct sbp_target_agent *);
  
  static void sbp_login_release(struct sbp_login_descriptor *login,
  	bool cancel_work)
  {
  	struct sbp_session *sess = login->sess;
  
  	/* FIXME: abort/wait on tasks */
  
  	sbp_target_agent_unregister(login->tgt_agt);
  
  	if (sess) {
  		spin_lock_bh(&sess->lock);
  		list_del(&login->link);
  		spin_unlock_bh(&sess->lock);
  
  		sbp_session_release(sess, cancel_work);
  	}
  
  	kfree(login);
  }
  
  static struct sbp_target_agent *sbp_target_agent_register(
  	struct sbp_login_descriptor *);
  
  static void sbp_management_request_login(
  	struct sbp_management_agent *agent, struct sbp_management_request *req,
  	int *status_data_size)
  {
  	struct sbp_tport *tport = agent->tport;
  	struct sbp_tpg *tpg = tport->tpg;
a511ce339   Chris Boot   sbp-target: Initi...
273
274
275
  	struct sbp_session *sess;
  	struct sbp_login_descriptor *login;
  	struct sbp_login_response_block *response;
6bb826121   Nicholas Bellinger   target: Convert s...
276
277
278
  	u64 guid;
  	u32 unpacked_lun;
  	int login_response_len, ret;
a511ce339   Chris Boot   sbp-target: Initi...
279

6bb826121   Nicholas Bellinger   target: Convert s...
280
281
282
  	unpacked_lun = sbp_get_lun_from_tpg(tpg,
  			LOGIN_ORB_LUN(be32_to_cpu(req->orb.misc)), &ret);
  	if (ret) {
a511ce339   Chris Boot   sbp-target: Initi...
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  		pr_notice("login to unknown LUN: %d
  ",
  			LOGIN_ORB_LUN(be32_to_cpu(req->orb.misc)));
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_LUN_NOTSUPP));
  		return;
  	}
  
  	ret = read_peer_guid(&guid, req);
  	if (ret != RCODE_COMPLETE) {
  		pr_warn("failed to read peer GUID: %d
  ", ret);
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR));
  		return;
  	}
  
  	pr_notice("mgt_agent LOGIN to LUN %d from %016llx
  ",
6bb826121   Nicholas Bellinger   target: Convert s...
306
  		unpacked_lun, guid);
a511ce339   Chris Boot   sbp-target: Initi...
307
308
309
  
  	sess = sbp_session_find_by_guid(tpg, guid);
  	if (sess) {
6bb826121   Nicholas Bellinger   target: Convert s...
310
  		login = sbp_login_find_by_lun(sess, unpacked_lun);
a511ce339   Chris Boot   sbp-target: Initi...
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
  		if (login) {
  			pr_notice("initiator already logged-in
  ");
  
  			/*
  			 * SBP-2 R4 says we should return access denied, but
  			 * that can confuse initiators. Instead we need to
  			 * treat this like a reconnect, but send the login
  			 * response block like a fresh login.
  			 *
  			 * This is required particularly in the case of Apple
  			 * devices booting off the FireWire target, where
  			 * the firmware has an active login to the target. When
  			 * the OS takes control of the session it issues its own
  			 * LOGIN rather than a RECONNECT. To avoid the machine
  			 * waiting until the reconnect_hold expires, we can skip
  			 * the ACCESS_DENIED errors to speed things up.
  			 */
  
  			goto already_logged_in;
  		}
  	}
  
  	/*
  	 * check exclusive bit in login request
  	 * reject with access_denied if any logins present
  	 */
  	if (LOGIN_ORB_EXCLUSIVE(be32_to_cpu(req->orb.misc)) &&
6bb826121   Nicholas Bellinger   target: Convert s...
339
  			sbp_login_count_all_by_lun(tpg, unpacked_lun, 0)) {
a511ce339   Chris Boot   sbp-target: Initi...
340
341
342
343
344
345
346
347
348
349
350
351
352
  		pr_warn("refusing exclusive login with other active logins
  ");
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED));
  		return;
  	}
  
  	/*
  	 * check exclusive bit in any existing login descriptor
  	 * reject with access_denied if any exclusive logins present
  	 */
6bb826121   Nicholas Bellinger   target: Convert s...
353
  	if (sbp_login_count_all_by_lun(tpg, unpacked_lun, 1)) {
a511ce339   Chris Boot   sbp-target: Initi...
354
355
356
357
358
359
360
361
362
363
364
365
366
  		pr_warn("refusing login while another exclusive login present
  ");
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED));
  		return;
  	}
  
  	/*
  	 * check we haven't exceeded the number of allowed logins
  	 * reject with resources_unavailable if we have
  	 */
6bb826121   Nicholas Bellinger   target: Convert s...
367
  	if (sbp_login_count_all_by_lun(tpg, unpacked_lun, 0) >=
a511ce339   Chris Boot   sbp-target: Initi...
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
  			tport->max_logins_per_lun) {
  		pr_warn("max number of logins reached
  ");
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL));
  		return;
  	}
  
  	if (!sess) {
  		sess = sbp_session_create(tpg, guid);
  		if (IS_ERR(sess)) {
  			switch (PTR_ERR(sess)) {
  			case -EPERM:
  				ret = SBP_STATUS_ACCESS_DENIED;
  				break;
  			default:
  				ret = SBP_STATUS_RESOURCES_UNAVAIL;
  				break;
  			}
  
  			req->status.status = cpu_to_be32(
  				STATUS_BLOCK_RESP(
  					STATUS_RESP_REQUEST_COMPLETE) |
  				STATUS_BLOCK_SBP_STATUS(ret));
  			return;
  		}
  
  		sess->node_id = req->node_addr;
  		sess->card = fw_card_get(req->card);
  		sess->generation = req->generation;
  		sess->speed = req->speed;
  
  		schedule_delayed_work(&sess->maint_work,
  				SESSION_MAINTENANCE_INTERVAL);
  	}
  
  	/* only take the latest reconnect_hold into account */
  	sess->reconnect_hold = min(
  		1 << LOGIN_ORB_RECONNECT(be32_to_cpu(req->orb.misc)),
  		tport->max_reconnect_timeout) - 1;
  
  	login = kmalloc(sizeof(*login), GFP_KERNEL);
  	if (!login) {
  		pr_err("failed to allocate login descriptor
  ");
  
  		sbp_session_release(sess, true);
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL));
  		return;
  	}
  
  	login->sess = sess;
6bb826121   Nicholas Bellinger   target: Convert s...
425
  	login->login_lun = unpacked_lun;
a511ce339   Chris Boot   sbp-target: Initi...
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
  	login->status_fifo_addr = sbp2_pointer_to_addr(&req->orb.status_fifo);
  	login->exclusive = LOGIN_ORB_EXCLUSIVE(be32_to_cpu(req->orb.misc));
  	login->login_id = atomic_inc_return(&login_id);
  
  	login->tgt_agt = sbp_target_agent_register(login);
  	if (IS_ERR(login->tgt_agt)) {
  		ret = PTR_ERR(login->tgt_agt);
  		pr_err("failed to map command block handler: %d
  ", ret);
  
  		sbp_session_release(sess, true);
  		kfree(login);
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL));
  		return;
  	}
  
  	spin_lock_bh(&sess->lock);
  	list_add_tail(&login->link, &sess->login_list);
  	spin_unlock_bh(&sess->lock);
  
  already_logged_in:
  	response = kzalloc(sizeof(*response), GFP_KERNEL);
  	if (!response) {
  		pr_err("failed to allocate login response block
  ");
  
  		sbp_login_release(login, true);
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_RESOURCES_UNAVAIL));
  		return;
  	}
  
  	login_response_len = clamp_val(
  			LOGIN_ORB_RESPONSE_LENGTH(be32_to_cpu(req->orb.length)),
  			12, sizeof(*response));
  	response->misc = cpu_to_be32(
  		((login_response_len & 0xffff) << 16) |
  		(login->login_id & 0xffff));
  	response->reconnect_hold = cpu_to_be32(sess->reconnect_hold & 0xffff);
  	addr_to_sbp2_pointer(login->tgt_agt->handler.offset,
  		&response->command_block_agent);
  
  	ret = sbp_run_transaction(sess->card, TCODE_WRITE_BLOCK_REQUEST,
  		sess->node_id, sess->generation, sess->speed,
  		sbp2_pointer_to_addr(&req->orb.ptr2), response,
  		login_response_len);
  	if (ret != RCODE_COMPLETE) {
  		pr_debug("failed to write login response block: %x
  ", ret);
  
  		kfree(response);
  		sbp_login_release(login, true);
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR));
  		return;
  	}
  
  	kfree(response);
  
  	req->status.status = cpu_to_be32(
  		STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  		STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK));
  }
  
  static void sbp_management_request_query_logins(
  	struct sbp_management_agent *agent, struct sbp_management_request *req,
  	int *status_data_size)
  {
  	pr_notice("QUERY LOGINS not implemented
  ");
  	/* FIXME: implement */
  
  	req->status.status = cpu_to_be32(
  		STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  		STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  }
  
  static void sbp_management_request_reconnect(
  	struct sbp_management_agent *agent, struct sbp_management_request *req,
  	int *status_data_size)
  {
  	struct sbp_tport *tport = agent->tport;
  	struct sbp_tpg *tpg = tport->tpg;
  	int ret;
  	u64 guid;
  	struct sbp_login_descriptor *login;
  
  	ret = read_peer_guid(&guid, req);
  	if (ret != RCODE_COMPLETE) {
  		pr_warn("failed to read peer GUID: %d
  ", ret);
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR));
  		return;
  	}
  
  	pr_notice("mgt_agent RECONNECT from %016llx
  ", guid);
  
  	login = sbp_login_find_by_id(tpg,
  		RECONNECT_ORB_LOGIN_ID(be32_to_cpu(req->orb.misc)));
  
  	if (!login) {
  		pr_err("mgt_agent RECONNECT unknown login ID
  ");
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED));
  		return;
  	}
  
  	if (login->sess->guid != guid) {
  		pr_err("mgt_agent RECONNECT login GUID doesn't match
  ");
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED));
  		return;
  	}
  
  	spin_lock_bh(&login->sess->lock);
  	if (login->sess->card)
  		fw_card_put(login->sess->card);
  
  	/* update the node details */
  	login->sess->generation = req->generation;
  	login->sess->node_id = req->node_addr;
  	login->sess->card = fw_card_get(req->card);
  	login->sess->speed = req->speed;
  	spin_unlock_bh(&login->sess->lock);
  
  	req->status.status = cpu_to_be32(
  		STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  		STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK));
  }
  
  static void sbp_management_request_logout(
  	struct sbp_management_agent *agent, struct sbp_management_request *req,
  	int *status_data_size)
  {
  	struct sbp_tport *tport = agent->tport;
  	struct sbp_tpg *tpg = tport->tpg;
5f2a3d619   Stefan Richter   sbp-target: renam...
579
  	int id;
a511ce339   Chris Boot   sbp-target: Initi...
580
  	struct sbp_login_descriptor *login;
5f2a3d619   Stefan Richter   sbp-target: renam...
581
  	id = LOGOUT_ORB_LOGIN_ID(be32_to_cpu(req->orb.misc));
a511ce339   Chris Boot   sbp-target: Initi...
582

5f2a3d619   Stefan Richter   sbp-target: renam...
583
  	login = sbp_login_find_by_id(tpg, id);
a511ce339   Chris Boot   sbp-target: Initi...
584
  	if (!login) {
5f2a3d619   Stefan Richter   sbp-target: renam...
585
586
  		pr_warn("cannot find login: %d
  ", id);
a511ce339   Chris Boot   sbp-target: Initi...
587
588
589
590
591
592
593
594
595
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_LOGIN_ID_UNKNOWN));
  		return;
  	}
  
  	pr_info("mgt_agent LOGOUT from LUN %d session %d
  ",
6bb826121   Nicholas Bellinger   target: Convert s...
596
  		login->login_lun, login->login_id);
a511ce339   Chris Boot   sbp-target: Initi...
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
  
  	if (req->node_addr != login->sess->node_id) {
  		pr_warn("logout from different node ID
  ");
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_ACCESS_DENIED));
  		return;
  	}
  
  	sbp_login_release(login, true);
  
  	req->status.status = cpu_to_be32(
  		STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  		STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK));
  }
  
  static void session_check_for_reset(struct sbp_session *sess)
  {
  	bool card_valid = false;
  
  	spin_lock_bh(&sess->lock);
  
  	if (sess->card) {
  		spin_lock_irq(&sess->card->lock);
  		card_valid = (sess->card->local_node != NULL);
  		spin_unlock_irq(&sess->card->lock);
  
  		if (!card_valid) {
  			fw_card_put(sess->card);
  			sess->card = NULL;
  		}
  	}
  
  	if (!card_valid || (sess->generation != sess->card->generation)) {
  		pr_info("Waiting for reconnect from node: %016llx
  ",
  				sess->guid);
  
  		sess->node_id = -1;
  		sess->reconnect_expires = get_jiffies_64() +
  			((sess->reconnect_hold + 1) * HZ);
  	}
  
  	spin_unlock_bh(&sess->lock);
  }
  
  static void session_reconnect_expired(struct sbp_session *sess)
  {
  	struct sbp_login_descriptor *login, *temp;
  	LIST_HEAD(login_list);
  
  	pr_info("Reconnect timer expired for node: %016llx
  ", sess->guid);
  
  	spin_lock_bh(&sess->lock);
  	list_for_each_entry_safe(login, temp, &sess->login_list, link) {
  		login->sess = NULL;
bf11eefcb   Wei Yongjun   target: use list_...
656
  		list_move_tail(&login->link, &login_list);
a511ce339   Chris Boot   sbp-target: Initi...
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
  	}
  	spin_unlock_bh(&sess->lock);
  
  	list_for_each_entry_safe(login, temp, &login_list, link) {
  		list_del(&login->link);
  		sbp_login_release(login, false);
  	}
  
  	sbp_session_release(sess, false);
  }
  
  static void session_maintenance_work(struct work_struct *work)
  {
  	struct sbp_session *sess = container_of(work, struct sbp_session,
  			maint_work.work);
  
  	/* could be called while tearing down the session */
  	spin_lock_bh(&sess->lock);
  	if (list_empty(&sess->login_list)) {
  		spin_unlock_bh(&sess->lock);
  		return;
  	}
  	spin_unlock_bh(&sess->lock);
  
  	if (sess->node_id != -1) {
  		/* check for bus reset and make node_id invalid */
  		session_check_for_reset(sess);
  
  		schedule_delayed_work(&sess->maint_work,
  				SESSION_MAINTENANCE_INTERVAL);
  	} else if (!time_after64(get_jiffies_64(), sess->reconnect_expires)) {
  		/* still waiting for reconnect */
  		schedule_delayed_work(&sess->maint_work,
  				SESSION_MAINTENANCE_INTERVAL);
  	} else {
  		/* reconnect timeout has expired */
  		session_reconnect_expired(sess);
  	}
  }
  
  static int tgt_agent_rw_agent_state(struct fw_card *card, int tcode, void *data,
  		struct sbp_target_agent *agent)
  {
37419d674   Chris Boot   sbp-target: use s...
700
  	int state;
a511ce339   Chris Boot   sbp-target: Initi...
701
702
703
704
705
706
707
  
  	switch (tcode) {
  	case TCODE_READ_QUADLET_REQUEST:
  		pr_debug("tgt_agent AGENT_STATE READ
  ");
  
  		spin_lock_bh(&agent->lock);
37419d674   Chris Boot   sbp-target: use s...
708
  		state = agent->state;
a511ce339   Chris Boot   sbp-target: Initi...
709
  		spin_unlock_bh(&agent->lock);
37419d674   Chris Boot   sbp-target: use s...
710
711
  
  		*(__be32 *)data = cpu_to_be32(state);
a511ce339   Chris Boot   sbp-target: Initi...
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
  
  		return RCODE_COMPLETE;
  
  	case TCODE_WRITE_QUADLET_REQUEST:
  		/* ignored */
  		return RCODE_COMPLETE;
  
  	default:
  		return RCODE_TYPE_ERROR;
  	}
  }
  
  static int tgt_agent_rw_agent_reset(struct fw_card *card, int tcode, void *data,
  		struct sbp_target_agent *agent)
  {
  	switch (tcode) {
  	case TCODE_WRITE_QUADLET_REQUEST:
  		pr_debug("tgt_agent AGENT_RESET
  ");
  		spin_lock_bh(&agent->lock);
  		agent->state = AGENT_STATE_RESET;
  		spin_unlock_bh(&agent->lock);
  		return RCODE_COMPLETE;
  
  	default:
  		return RCODE_TYPE_ERROR;
  	}
  }
  
  static int tgt_agent_rw_orb_pointer(struct fw_card *card, int tcode, void *data,
  		struct sbp_target_agent *agent)
  {
  	struct sbp2_pointer *ptr = data;
  
  	switch (tcode) {
  	case TCODE_WRITE_BLOCK_REQUEST:
  		spin_lock_bh(&agent->lock);
  		if (agent->state != AGENT_STATE_SUSPENDED &&
  				agent->state != AGENT_STATE_RESET) {
  			spin_unlock_bh(&agent->lock);
  			pr_notice("Ignoring ORB_POINTER write while active.
  ");
  			return RCODE_CONFLICT_ERROR;
  		}
  		agent->state = AGENT_STATE_ACTIVE;
  		spin_unlock_bh(&agent->lock);
  
  		agent->orb_pointer = sbp2_pointer_to_addr(ptr);
  		agent->doorbell = false;
  
  		pr_debug("tgt_agent ORB_POINTER write: 0x%llx
  ",
  				agent->orb_pointer);
  
  		queue_work(system_unbound_wq, &agent->work);
  
  		return RCODE_COMPLETE;
  
  	case TCODE_READ_BLOCK_REQUEST:
  		pr_debug("tgt_agent ORB_POINTER READ
  ");
  		spin_lock_bh(&agent->lock);
  		addr_to_sbp2_pointer(agent->orb_pointer, ptr);
  		spin_unlock_bh(&agent->lock);
  		return RCODE_COMPLETE;
  
  	default:
  		return RCODE_TYPE_ERROR;
  	}
  }
  
  static int tgt_agent_rw_doorbell(struct fw_card *card, int tcode, void *data,
  		struct sbp_target_agent *agent)
  {
  	switch (tcode) {
  	case TCODE_WRITE_QUADLET_REQUEST:
  		spin_lock_bh(&agent->lock);
  		if (agent->state != AGENT_STATE_SUSPENDED) {
  			spin_unlock_bh(&agent->lock);
  			pr_debug("Ignoring DOORBELL while active.
  ");
  			return RCODE_CONFLICT_ERROR;
  		}
  		agent->state = AGENT_STATE_ACTIVE;
  		spin_unlock_bh(&agent->lock);
  
  		agent->doorbell = true;
  
  		pr_debug("tgt_agent DOORBELL
  ");
  
  		queue_work(system_unbound_wq, &agent->work);
  
  		return RCODE_COMPLETE;
  
  	case TCODE_READ_QUADLET_REQUEST:
  		return RCODE_COMPLETE;
  
  	default:
  		return RCODE_TYPE_ERROR;
  	}
  }
  
  static int tgt_agent_rw_unsolicited_status_enable(struct fw_card *card,
  		int tcode, void *data, struct sbp_target_agent *agent)
  {
  	switch (tcode) {
  	case TCODE_WRITE_QUADLET_REQUEST:
  		pr_debug("tgt_agent UNSOLICITED_STATUS_ENABLE
  ");
  		/* ignored as we don't send unsolicited status */
  		return RCODE_COMPLETE;
  
  	case TCODE_READ_QUADLET_REQUEST:
  		return RCODE_COMPLETE;
  
  	default:
  		return RCODE_TYPE_ERROR;
  	}
  }
  
  static void tgt_agent_rw(struct fw_card *card, struct fw_request *request,
  		int tcode, int destination, int source, int generation,
  		unsigned long long offset, void *data, size_t length,
  		void *callback_data)
  {
  	struct sbp_target_agent *agent = callback_data;
  	struct sbp_session *sess = agent->login->sess;
  	int sess_gen, sess_node, rcode;
  
  	spin_lock_bh(&sess->lock);
  	sess_gen = sess->generation;
  	sess_node = sess->node_id;
  	spin_unlock_bh(&sess->lock);
  
  	if (generation != sess_gen) {
  		pr_notice("ignoring request with wrong generation
  ");
  		rcode = RCODE_TYPE_ERROR;
  		goto out;
  	}
  
  	if (source != sess_node) {
  		pr_notice("ignoring request from foreign node (%x != %x)
  ",
  				source, sess_node);
  		rcode = RCODE_TYPE_ERROR;
  		goto out;
  	}
  
  	/* turn offset into the offset from the start of the block */
  	offset -= agent->handler.offset;
  
  	if (offset == 0x00 && length == 4) {
  		/* AGENT_STATE */
  		rcode = tgt_agent_rw_agent_state(card, tcode, data, agent);
  	} else if (offset == 0x04 && length == 4) {
  		/* AGENT_RESET */
  		rcode = tgt_agent_rw_agent_reset(card, tcode, data, agent);
  	} else if (offset == 0x08 && length == 8) {
  		/* ORB_POINTER */
  		rcode = tgt_agent_rw_orb_pointer(card, tcode, data, agent);
  	} else if (offset == 0x10 && length == 4) {
  		/* DOORBELL */
  		rcode = tgt_agent_rw_doorbell(card, tcode, data, agent);
  	} else if (offset == 0x14 && length == 4) {
  		/* UNSOLICITED_STATUS_ENABLE */
  		rcode = tgt_agent_rw_unsolicited_status_enable(card, tcode,
  				data, agent);
  	} else {
  		rcode = RCODE_ADDRESS_ERROR;
  	}
  
  out:
  	fw_send_response(card, request, rcode);
  }
  
  static void sbp_handle_command(struct sbp_target_request *);
  static int sbp_send_status(struct sbp_target_request *);
  static void sbp_free_request(struct sbp_target_request *);
  
  static void tgt_agent_process_work(struct work_struct *work)
  {
  	struct sbp_target_request *req =
  		container_of(work, struct sbp_target_request, work);
  
  	pr_debug("tgt_orb ptr:0x%llx next_ORB:0x%llx data_descriptor:0x%llx misc:0x%x
  ",
  			req->orb_pointer,
  			sbp2_pointer_to_addr(&req->orb.next_orb),
  			sbp2_pointer_to_addr(&req->orb.data_descriptor),
  			be32_to_cpu(req->orb.misc));
  
  	if (req->orb_pointer >> 32)
  		pr_debug("ORB with high bits set
  ");
  
  	switch (ORB_REQUEST_FORMAT(be32_to_cpu(req->orb.misc))) {
  		case 0:/* Format specified by this standard */
  			sbp_handle_command(req);
  			return;
  		case 1: /* Reserved for future standardization */
  		case 2: /* Vendor-dependent */
  			req->status.status |= cpu_to_be32(
  					STATUS_BLOCK_RESP(
  						STATUS_RESP_REQUEST_COMPLETE) |
  					STATUS_BLOCK_DEAD(0) |
  					STATUS_BLOCK_LEN(1) |
  					STATUS_BLOCK_SBP_STATUS(
  						SBP_STATUS_REQ_TYPE_NOTSUPP));
  			sbp_send_status(req);
a511ce339   Chris Boot   sbp-target: Initi...
923
924
925
926
927
928
929
930
931
932
  			return;
  		case 3: /* Dummy ORB */
  			req->status.status |= cpu_to_be32(
  					STATUS_BLOCK_RESP(
  						STATUS_RESP_REQUEST_COMPLETE) |
  					STATUS_BLOCK_DEAD(0) |
  					STATUS_BLOCK_LEN(1) |
  					STATUS_BLOCK_SBP_STATUS(
  						SBP_STATUS_DUMMY_ORB_COMPLETE));
  			sbp_send_status(req);
a511ce339   Chris Boot   sbp-target: Initi...
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
  			return;
  		default:
  			BUG();
  	}
  }
  
  /* used to double-check we haven't been issued an AGENT_RESET */
  static inline bool tgt_agent_check_active(struct sbp_target_agent *agent)
  {
  	bool active;
  
  	spin_lock_bh(&agent->lock);
  	active = (agent->state == AGENT_STATE_ACTIVE);
  	spin_unlock_bh(&agent->lock);
  
  	return active;
  }
5a3ee221b   Nicholas Bellinger   sbp-target: Conve...
950
951
952
953
954
  static struct sbp_target_request *sbp_mgt_get_req(struct sbp_session *sess,
  	struct fw_card *card, u64 next_orb)
  {
  	struct se_session *se_sess = sess->se_sess;
  	struct sbp_target_request *req;
10e9cbb6b   Matthew Wilcox   scsi: target: Con...
955
  	int tag, cpu;
5a3ee221b   Nicholas Bellinger   sbp-target: Conve...
956

10e9cbb6b   Matthew Wilcox   scsi: target: Con...
957
  	tag = sbitmap_queue_get(&se_sess->sess_tag_pool, &cpu);
5a3ee221b   Nicholas Bellinger   sbp-target: Conve...
958
959
960
961
962
963
  	if (tag < 0)
  		return ERR_PTR(-ENOMEM);
  
  	req = &((struct sbp_target_request *)se_sess->sess_cmd_map)[tag];
  	memset(req, 0, sizeof(*req));
  	req->se_cmd.map_tag = tag;
10e9cbb6b   Matthew Wilcox   scsi: target: Con...
964
  	req->se_cmd.map_cpu = cpu;
5a3ee221b   Nicholas Bellinger   sbp-target: Conve...
965
966
967
968
  	req->se_cmd.tag = next_orb;
  
  	return req;
  }
a511ce339   Chris Boot   sbp-target: Initi...
969
970
971
972
973
974
975
976
977
978
979
  static void tgt_agent_fetch_work(struct work_struct *work)
  {
  	struct sbp_target_agent *agent =
  		container_of(work, struct sbp_target_agent, work);
  	struct sbp_session *sess = agent->login->sess;
  	struct sbp_target_request *req;
  	int ret;
  	bool doorbell = agent->doorbell;
  	u64 next_orb = agent->orb_pointer;
  
  	while (next_orb && tgt_agent_check_active(agent)) {
5a3ee221b   Nicholas Bellinger   sbp-target: Conve...
980
981
  		req = sbp_mgt_get_req(sess, sess->card, next_orb);
  		if (IS_ERR(req)) {
a511ce339   Chris Boot   sbp-target: Initi...
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
  			spin_lock_bh(&agent->lock);
  			agent->state = AGENT_STATE_DEAD;
  			spin_unlock_bh(&agent->lock);
  			return;
  		}
  
  		req->login = agent->login;
  		req->orb_pointer = next_orb;
  
  		req->status.status = cpu_to_be32(STATUS_BLOCK_ORB_OFFSET_HIGH(
  					req->orb_pointer >> 32));
  		req->status.orb_low = cpu_to_be32(
  				req->orb_pointer & 0xfffffffc);
  
  		/* read in the ORB */
  		ret = sbp_run_transaction(sess->card, TCODE_READ_BLOCK_REQUEST,
  				sess->node_id, sess->generation, sess->speed,
  				req->orb_pointer, &req->orb, sizeof(req->orb));
  		if (ret != RCODE_COMPLETE) {
  			pr_debug("tgt_orb fetch failed: %x
  ", ret);
  			req->status.status |= cpu_to_be32(
  					STATUS_BLOCK_SRC(
  						STATUS_SRC_ORB_FINISHED) |
  					STATUS_BLOCK_RESP(
  						STATUS_RESP_TRANSPORT_FAILURE) |
  					STATUS_BLOCK_DEAD(1) |
  					STATUS_BLOCK_LEN(1) |
  					STATUS_BLOCK_SBP_STATUS(
  						SBP_STATUS_UNSPECIFIED_ERROR));
  			spin_lock_bh(&agent->lock);
  			agent->state = AGENT_STATE_DEAD;
  			spin_unlock_bh(&agent->lock);
  
  			sbp_send_status(req);
a511ce339   Chris Boot   sbp-target: Initi...
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
  			return;
  		}
  
  		/* check the next_ORB field */
  		if (be32_to_cpu(req->orb.next_orb.high) & 0x80000000) {
  			next_orb = 0;
  			req->status.status |= cpu_to_be32(STATUS_BLOCK_SRC(
  						STATUS_SRC_ORB_FINISHED));
  		} else {
  			next_orb = sbp2_pointer_to_addr(&req->orb.next_orb);
  			req->status.status |= cpu_to_be32(STATUS_BLOCK_SRC(
  						STATUS_SRC_ORB_CONTINUING));
  		}
  
  		if (tgt_agent_check_active(agent) && !doorbell) {
  			INIT_WORK(&req->work, tgt_agent_process_work);
  			queue_work(system_unbound_wq, &req->work);
  		} else {
  			/* don't process this request, just check next_ORB */
  			sbp_free_request(req);
  		}
  
  		spin_lock_bh(&agent->lock);
  		doorbell = agent->doorbell = false;
  
  		/* check if we should carry on processing */
  		if (next_orb)
  			agent->orb_pointer = next_orb;
  		else
  			agent->state = AGENT_STATE_SUSPENDED;
  
  		spin_unlock_bh(&agent->lock);
  	};
  }
  
  static struct sbp_target_agent *sbp_target_agent_register(
  		struct sbp_login_descriptor *login)
  {
  	struct sbp_target_agent *agent;
  	int ret;
  
  	agent = kmalloc(sizeof(*agent), GFP_KERNEL);
  	if (!agent)
  		return ERR_PTR(-ENOMEM);
  
  	spin_lock_init(&agent->lock);
  
  	agent->handler.length = 0x20;
  	agent->handler.address_callback = tgt_agent_rw;
  	agent->handler.callback_data = agent;
  
  	agent->login = login;
  	agent->state = AGENT_STATE_RESET;
  	INIT_WORK(&agent->work, tgt_agent_fetch_work);
  	agent->orb_pointer = 0;
  	agent->doorbell = false;
  
  	ret = fw_core_add_address_handler(&agent->handler,
  			&sbp_register_region);
  	if (ret < 0) {
  		kfree(agent);
  		return ERR_PTR(ret);
  	}
  
  	return agent;
  }
  
  static void sbp_target_agent_unregister(struct sbp_target_agent *agent)
  {
  	fw_core_remove_address_handler(&agent->handler);
  	cancel_work_sync(&agent->work);
  	kfree(agent);
  }
  
  /*
   * Simple wrapper around fw_run_transaction that retries the transaction several
   * times in case of failure, with an exponential backoff.
   */
  static int sbp_run_transaction(struct fw_card *card, int tcode, int destination_id,
  		int generation, int speed, unsigned long long offset,
  		void *payload, size_t length)
  {
  	int attempt, ret, delay;
  
  	for (attempt = 1; attempt <= 5; attempt++) {
  		ret = fw_run_transaction(card, tcode, destination_id,
  				generation, speed, offset, payload, length);
  
  		switch (ret) {
  		case RCODE_COMPLETE:
  		case RCODE_TYPE_ERROR:
  		case RCODE_ADDRESS_ERROR:
  		case RCODE_GENERATION:
  			return ret;
  
  		default:
  			delay = 5 * attempt * attempt;
  			usleep_range(delay, delay * 2);
  		}
  	}
  
  	return ret;
  }
  
  /*
   * Wrapper around sbp_run_transaction that gets the card, destination,
   * generation and speed out of the request's session.
   */
  static int sbp_run_request_transaction(struct sbp_target_request *req,
  		int tcode, unsigned long long offset, void *payload,
  		size_t length)
  {
  	struct sbp_login_descriptor *login = req->login;
  	struct sbp_session *sess = login->sess;
  	struct fw_card *card;
  	int node_id, generation, speed, ret;
  
  	spin_lock_bh(&sess->lock);
  	card = fw_card_get(sess->card);
  	node_id = sess->node_id;
  	generation = sess->generation;
  	speed = sess->speed;
  	spin_unlock_bh(&sess->lock);
  
  	ret = sbp_run_transaction(card, tcode, node_id, generation, speed,
  			offset, payload, length);
  
  	fw_card_put(card);
  
  	return ret;
  }
  
  static int sbp_fetch_command(struct sbp_target_request *req)
  {
  	int ret, cmd_len, copy_len;
  
  	cmd_len = scsi_command_size(req->orb.command_block);
  
  	req->cmd_buf = kmalloc(cmd_len, GFP_KERNEL);
  	if (!req->cmd_buf)
  		return -ENOMEM;
  
  	memcpy(req->cmd_buf, req->orb.command_block,
  		min_t(int, cmd_len, sizeof(req->orb.command_block)));
  
  	if (cmd_len > sizeof(req->orb.command_block)) {
  		pr_debug("sbp_fetch_command: filling in long command
  ");
  		copy_len = cmd_len - sizeof(req->orb.command_block);
  
  		ret = sbp_run_request_transaction(req,
  				TCODE_READ_BLOCK_REQUEST,
  				req->orb_pointer + sizeof(req->orb),
  				req->cmd_buf + sizeof(req->orb.command_block),
  				copy_len);
  		if (ret != RCODE_COMPLETE)
  			return -EIO;
  	}
  
  	return 0;
  }
  
  static int sbp_fetch_page_table(struct sbp_target_request *req)
  {
  	int pg_tbl_sz, ret;
  	struct sbp_page_table_entry *pg_tbl;
  
  	if (!CMDBLK_ORB_PG_TBL_PRESENT(be32_to_cpu(req->orb.misc)))
  		return 0;
  
  	pg_tbl_sz = CMDBLK_ORB_DATA_SIZE(be32_to_cpu(req->orb.misc)) *
  		sizeof(struct sbp_page_table_entry);
  
  	pg_tbl = kmalloc(pg_tbl_sz, GFP_KERNEL);
  	if (!pg_tbl)
  		return -ENOMEM;
  
  	ret = sbp_run_request_transaction(req, TCODE_READ_BLOCK_REQUEST,
  			sbp2_pointer_to_addr(&req->orb.data_descriptor),
  			pg_tbl, pg_tbl_sz);
  	if (ret != RCODE_COMPLETE) {
  		kfree(pg_tbl);
  		return -EIO;
  	}
  
  	req->pg_tbl = pg_tbl;
  	return 0;
  }
  
  static void sbp_calc_data_length_direction(struct sbp_target_request *req,
  	u32 *data_len, enum dma_data_direction *data_dir)
  {
  	int data_size, direction, idx;
  
  	data_size = CMDBLK_ORB_DATA_SIZE(be32_to_cpu(req->orb.misc));
  	direction = CMDBLK_ORB_DIRECTION(be32_to_cpu(req->orb.misc));
  
  	if (!data_size) {
  		*data_len = 0;
  		*data_dir = DMA_NONE;
  		return;
  	}
  
  	*data_dir = direction ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
  
  	if (req->pg_tbl) {
  		*data_len = 0;
  		for (idx = 0; idx < data_size; idx++) {
  			*data_len += be16_to_cpu(
  					req->pg_tbl[idx].segment_length);
  		}
  	} else {
  		*data_len = data_size;
  	}
  }
  
  static void sbp_handle_command(struct sbp_target_request *req)
  {
  	struct sbp_login_descriptor *login = req->login;
  	struct sbp_session *sess = login->sess;
  	int ret, unpacked_lun;
  	u32 data_length;
  	enum dma_data_direction data_dir;
  
  	ret = sbp_fetch_command(req);
  	if (ret) {
  		pr_debug("sbp_handle_command: fetch command failed: %d
  ", ret);
7c78b8de2   Roland Dreier   sbp-target: Conso...
1245
  		goto err;
a511ce339   Chris Boot   sbp-target: Initi...
1246
1247
1248
1249
1250
1251
1252
  	}
  
  	ret = sbp_fetch_page_table(req);
  	if (ret) {
  		pr_debug("sbp_handle_command: fetch page table failed: %d
  ",
  			ret);
7c78b8de2   Roland Dreier   sbp-target: Conso...
1253
  		goto err;
a511ce339   Chris Boot   sbp-target: Initi...
1254
  	}
6bb826121   Nicholas Bellinger   target: Convert s...
1255
  	unpacked_lun = req->login->login_lun;
a511ce339   Chris Boot   sbp-target: Initi...
1256
1257
1258
1259
1260
  	sbp_calc_data_length_direction(req, &data_length, &data_dir);
  
  	pr_debug("sbp_handle_command ORB:0x%llx unpacked_lun:%d data_len:%d data_dir:%d
  ",
  			req->orb_pointer, unpacked_lun, data_length, data_dir);
649ee0549   Bart Van Assche   target: Move task...
1261
1262
  	/* only used for printk until we do TMRs */
  	req->se_cmd.tag = req->orb_pointer;
d6dfc868b   Roland Dreier   target: Allow for...
1263
1264
  	if (target_submit_cmd(&req->se_cmd, sess->se_sess, req->cmd_buf,
  			      req->sense_buf, unpacked_lun, data_length,
5f27edad9   Nicholas Bellinger   sbp-target: Conve...
1265
  			      TCM_SIMPLE_TAG, data_dir, TARGET_SCF_ACK_KREF))
d6dfc868b   Roland Dreier   target: Allow for...
1266
  		goto err;
7c78b8de2   Roland Dreier   sbp-target: Conso...
1267
1268
1269
1270
1271
1272
1273
1274
1275
  	return;
  
  err:
  	req->status.status |= cpu_to_be32(
  		STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) |
  		STATUS_BLOCK_DEAD(0) |
  		STATUS_BLOCK_LEN(1) |
  		STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR));
  	sbp_send_status(req);
a511ce339   Chris Boot   sbp-target: Initi...
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
  }
  
  /*
   * DMA_TO_DEVICE = read from initiator (SCSI WRITE)
   * DMA_FROM_DEVICE = write to initiator (SCSI READ)
   */
  static int sbp_rw_data(struct sbp_target_request *req)
  {
  	struct sbp_session *sess = req->login->sess;
  	int tcode, sg_miter_flags, max_payload, pg_size, speed, node_id,
  		generation, num_pte, length, tfr_length,
  		rcode = RCODE_COMPLETE;
  	struct sbp_page_table_entry *pte;
  	unsigned long long offset;
  	struct fw_card *card;
  	struct sg_mapping_iter iter;
  
  	if (req->se_cmd.data_direction == DMA_FROM_DEVICE) {
  		tcode = TCODE_WRITE_BLOCK_REQUEST;
  		sg_miter_flags = SG_MITER_FROM_SG;
  	} else {
  		tcode = TCODE_READ_BLOCK_REQUEST;
  		sg_miter_flags = SG_MITER_TO_SG;
  	}
  
  	max_payload = 4 << CMDBLK_ORB_MAX_PAYLOAD(be32_to_cpu(req->orb.misc));
  	speed = CMDBLK_ORB_SPEED(be32_to_cpu(req->orb.misc));
  
  	pg_size = CMDBLK_ORB_PG_SIZE(be32_to_cpu(req->orb.misc));
  	if (pg_size) {
  		pr_err("sbp_run_transaction: page size ignored
  ");
  		pg_size = 0x100 << pg_size;
  	}
  
  	spin_lock_bh(&sess->lock);
  	card = fw_card_get(sess->card);
  	node_id = sess->node_id;
  	generation = sess->generation;
  	spin_unlock_bh(&sess->lock);
  
  	if (req->pg_tbl) {
  		pte = req->pg_tbl;
  		num_pte = CMDBLK_ORB_DATA_SIZE(be32_to_cpu(req->orb.misc));
  
  		offset = 0;
  		length = 0;
  	} else {
  		pte = NULL;
  		num_pte = 0;
  
  		offset = sbp2_pointer_to_addr(&req->orb.data_descriptor);
  		length = req->se_cmd.data_length;
  	}
  
  	sg_miter_start(&iter, req->se_cmd.t_data_sg, req->se_cmd.t_data_nents,
  		sg_miter_flags);
  
  	while (length || num_pte) {
  		if (!length) {
  			offset = (u64)be16_to_cpu(pte->segment_base_hi) << 32 |
  				be32_to_cpu(pte->segment_base_lo);
  			length = be16_to_cpu(pte->segment_length);
  
  			pte++;
  			num_pte--;
  		}
  
  		sg_miter_next(&iter);
  
  		tfr_length = min3(length, max_payload, (int)iter.length);
  
  		/* FIXME: take page_size into account */
  
  		rcode = sbp_run_transaction(card, tcode, node_id,
  				generation, speed,
  				offset, iter.addr, tfr_length);
  
  		if (rcode != RCODE_COMPLETE)
  			break;
  
  		length -= tfr_length;
  		offset += tfr_length;
  		iter.consumed = tfr_length;
  	}
  
  	sg_miter_stop(&iter);
  	fw_card_put(card);
  
  	if (rcode == RCODE_COMPLETE) {
  		WARN_ON(length != 0);
  		return 0;
  	} else {
  		return -EIO;
  	}
  }
  
  static int sbp_send_status(struct sbp_target_request *req)
  {
5f27edad9   Nicholas Bellinger   sbp-target: Conve...
1375
  	int rc, ret = 0, length;
a511ce339   Chris Boot   sbp-target: Initi...
1376
1377
1378
  	struct sbp_login_descriptor *login = req->login;
  
  	length = (((be32_to_cpu(req->status.status) >> 24) & 0x07) + 1) * 4;
5f27edad9   Nicholas Bellinger   sbp-target: Conve...
1379
  	rc = sbp_run_request_transaction(req, TCODE_WRITE_BLOCK_REQUEST,
a511ce339   Chris Boot   sbp-target: Initi...
1380
  			login->status_fifo_addr, &req->status, length);
5f27edad9   Nicholas Bellinger   sbp-target: Conve...
1381
1382
1383
1384
1385
  	if (rc != RCODE_COMPLETE) {
  		pr_debug("sbp_send_status: write failed: 0x%x
  ", rc);
  		ret = -EIO;
  		goto put_ref;
a511ce339   Chris Boot   sbp-target: Initi...
1386
1387
1388
1389
1390
  	}
  
  	pr_debug("sbp_send_status: status write complete for ORB: 0x%llx
  ",
  			req->orb_pointer);
5f27edad9   Nicholas Bellinger   sbp-target: Conve...
1391
1392
1393
1394
1395
1396
1397
1398
  	/*
  	 * Drop the extra ACK_KREF reference taken by target_submit_cmd()
  	 * ahead of sbp_check_stop_free() -> transport_generic_free_cmd()
  	 * final se_cmd->cmd_kref put.
  	 */
  put_ref:
  	target_put_sess_cmd(&req->se_cmd);
  	return ret;
a511ce339   Chris Boot   sbp-target: Initi...
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
  }
  
  static void sbp_sense_mangle(struct sbp_target_request *req)
  {
  	struct se_cmd *se_cmd = &req->se_cmd;
  	u8 *sense = req->sense_buf;
  	u8 *status = req->status.data;
  
  	WARN_ON(se_cmd->scsi_sense_length < 18);
  
  	switch (sense[0] & 0x7f) { 		/* sfmt */
  	case 0x70: /* current, fixed */
  		status[0] = 0 << 6;
  		break;
  	case 0x71: /* deferred, fixed */
  		status[0] = 1 << 6;
  		break;
  	case 0x72: /* current, descriptor */
  	case 0x73: /* deferred, descriptor */
  	default:
  		/*
  		 * TODO: SBP-3 specifies what we should do with descriptor
  		 * format sense data
  		 */
  		pr_err("sbp_send_sense: unknown sense format: 0x%x
  ",
  			sense[0]);
  		req->status.status |= cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_DEAD(0) |
  			STATUS_BLOCK_LEN(1) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQUEST_ABORTED));
  		return;
  	}
  
  	status[0] |= se_cmd->scsi_status & 0x3f;/* status */
  	status[1] =
  		(sense[0] & 0x80) |		/* valid */
  		((sense[2] & 0xe0) >> 1) |	/* mark, eom, ili */
  		(sense[2] & 0x0f);		/* sense_key */
  	status[2] = se_cmd->scsi_asc;		/* sense_code */
  	status[3] = se_cmd->scsi_ascq;		/* sense_qualifier */
  
  	/* information */
  	status[4] = sense[3];
  	status[5] = sense[4];
  	status[6] = sense[5];
  	status[7] = sense[6];
  
  	/* CDB-dependent */
  	status[8] = sense[8];
  	status[9] = sense[9];
  	status[10] = sense[10];
  	status[11] = sense[11];
  
  	/* fru */
  	status[12] = sense[14];
  
  	/* sense_key-dependent */
  	status[13] = sense[15];
  	status[14] = sense[16];
  	status[15] = sense[17];
  
  	req->status.status |= cpu_to_be32(
  		STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  		STATUS_BLOCK_DEAD(0) |
  		STATUS_BLOCK_LEN(5) |
  		STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK));
  }
  
  static int sbp_send_sense(struct sbp_target_request *req)
  {
  	struct se_cmd *se_cmd = &req->se_cmd;
  
  	if (se_cmd->scsi_sense_length) {
  		sbp_sense_mangle(req);
  	} else {
  		req->status.status |= cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_DEAD(0) |
  			STATUS_BLOCK_LEN(1) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_OK));
  	}
  
  	return sbp_send_status(req);
  }
  
  static void sbp_free_request(struct sbp_target_request *req)
  {
5a3ee221b   Nicholas Bellinger   sbp-target: Conve...
1488
1489
  	struct se_cmd *se_cmd = &req->se_cmd;
  	struct se_session *se_sess = se_cmd->se_sess;
a511ce339   Chris Boot   sbp-target: Initi...
1490
1491
  	kfree(req->pg_tbl);
  	kfree(req->cmd_buf);
5a3ee221b   Nicholas Bellinger   sbp-target: Conve...
1492

83c2b54b9   Matthew Wilcox   scsi: target: Abs...
1493
  	target_free_tag(se_sess, se_cmd);
a511ce339   Chris Boot   sbp-target: Initi...
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
  }
  
  static void sbp_mgt_agent_process(struct work_struct *work)
  {
  	struct sbp_management_agent *agent =
  		container_of(work, struct sbp_management_agent, work);
  	struct sbp_management_request *req = agent->request;
  	int ret;
  	int status_data_len = 0;
  
  	/* fetch the ORB from the initiator */
  	ret = sbp_run_transaction(req->card, TCODE_READ_BLOCK_REQUEST,
  		req->node_addr, req->generation, req->speed,
  		agent->orb_offset, &req->orb, sizeof(req->orb));
  	if (ret != RCODE_COMPLETE) {
  		pr_debug("mgt_orb fetch failed: %x
  ", ret);
  		goto out;
  	}
  
  	pr_debug("mgt_orb ptr1:0x%llx ptr2:0x%llx misc:0x%x len:0x%x status_fifo:0x%llx
  ",
  		sbp2_pointer_to_addr(&req->orb.ptr1),
  		sbp2_pointer_to_addr(&req->orb.ptr2),
  		be32_to_cpu(req->orb.misc), be32_to_cpu(req->orb.length),
  		sbp2_pointer_to_addr(&req->orb.status_fifo));
  
  	if (!ORB_NOTIFY(be32_to_cpu(req->orb.misc)) ||
  		ORB_REQUEST_FORMAT(be32_to_cpu(req->orb.misc)) != 0) {
  		pr_err("mgt_orb bad request
  ");
  		goto out;
  	}
  
  	switch (MANAGEMENT_ORB_FUNCTION(be32_to_cpu(req->orb.misc))) {
  	case MANAGEMENT_ORB_FUNCTION_LOGIN:
  		sbp_management_request_login(agent, req, &status_data_len);
  		break;
  
  	case MANAGEMENT_ORB_FUNCTION_QUERY_LOGINS:
  		sbp_management_request_query_logins(agent, req,
  				&status_data_len);
  		break;
  
  	case MANAGEMENT_ORB_FUNCTION_RECONNECT:
  		sbp_management_request_reconnect(agent, req, &status_data_len);
  		break;
  
  	case MANAGEMENT_ORB_FUNCTION_SET_PASSWORD:
  		pr_notice("SET PASSWORD not implemented
  ");
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  
  		break;
  
  	case MANAGEMENT_ORB_FUNCTION_LOGOUT:
  		sbp_management_request_logout(agent, req, &status_data_len);
  		break;
  
  	case MANAGEMENT_ORB_FUNCTION_ABORT_TASK:
  		pr_notice("ABORT TASK not implemented
  ");
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  
  		break;
  
  	case MANAGEMENT_ORB_FUNCTION_ABORT_TASK_SET:
  		pr_notice("ABORT TASK SET not implemented
  ");
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  
  		break;
  
  	case MANAGEMENT_ORB_FUNCTION_LOGICAL_UNIT_RESET:
  		pr_notice("LOGICAL UNIT RESET not implemented
  ");
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  
  		break;
  
  	case MANAGEMENT_ORB_FUNCTION_TARGET_RESET:
  		pr_notice("TARGET RESET not implemented
  ");
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  
  		break;
  
  	default:
  		pr_notice("unknown management function 0x%x
  ",
  			MANAGEMENT_ORB_FUNCTION(be32_to_cpu(req->orb.misc)));
  
  		req->status.status = cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_REQUEST_COMPLETE) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_REQ_TYPE_NOTSUPP));
  
  		break;
  	}
  
  	req->status.status |= cpu_to_be32(
  		STATUS_BLOCK_SRC(1) | /* Response to ORB, next_ORB absent */
  		STATUS_BLOCK_LEN(DIV_ROUND_UP(status_data_len, 4) + 1) |
  		STATUS_BLOCK_ORB_OFFSET_HIGH(agent->orb_offset >> 32));
  	req->status.orb_low = cpu_to_be32(agent->orb_offset);
  
  	/* write the status block back to the initiator */
  	ret = sbp_run_transaction(req->card, TCODE_WRITE_BLOCK_REQUEST,
  		req->node_addr, req->generation, req->speed,
  		sbp2_pointer_to_addr(&req->orb.status_fifo),
  		&req->status, 8 + status_data_len);
  	if (ret != RCODE_COMPLETE) {
  		pr_debug("mgt_orb status write failed: %x
  ", ret);
  		goto out;
  	}
  
  out:
  	fw_card_put(req->card);
  	kfree(req);
  
  	spin_lock_bh(&agent->lock);
  	agent->state = MANAGEMENT_AGENT_STATE_IDLE;
  	spin_unlock_bh(&agent->lock);
  }
  
  static void sbp_mgt_agent_rw(struct fw_card *card,
  	struct fw_request *request, int tcode, int destination, int source,
  	int generation, unsigned long long offset, void *data, size_t length,
  	void *callback_data)
  {
  	struct sbp_management_agent *agent = callback_data;
  	struct sbp2_pointer *ptr = data;
  	int rcode = RCODE_ADDRESS_ERROR;
  
  	if (!agent->tport->enable)
  		goto out;
  
  	if ((offset != agent->handler.offset) || (length != 8))
  		goto out;
  
  	if (tcode == TCODE_WRITE_BLOCK_REQUEST) {
  		struct sbp_management_request *req;
  		int prev_state;
  
  		spin_lock_bh(&agent->lock);
  		prev_state = agent->state;
  		agent->state = MANAGEMENT_AGENT_STATE_BUSY;
  		spin_unlock_bh(&agent->lock);
  
  		if (prev_state == MANAGEMENT_AGENT_STATE_BUSY) {
  			pr_notice("ignoring management request while busy
  ");
  			rcode = RCODE_CONFLICT_ERROR;
  			goto out;
  		}
a511ce339   Chris Boot   sbp-target: Initi...
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
  		req = kzalloc(sizeof(*req), GFP_ATOMIC);
  		if (!req) {
  			rcode = RCODE_CONFLICT_ERROR;
  			goto out;
  		}
  
  		req->card = fw_card_get(card);
  		req->generation = generation;
  		req->node_addr = source;
  		req->speed = fw_get_request_speed(request);
  
  		agent->orb_offset = sbp2_pointer_to_addr(ptr);
  		agent->request = req;
  
  		queue_work(system_unbound_wq, &agent->work);
  		rcode = RCODE_COMPLETE;
  	} else if (tcode == TCODE_READ_BLOCK_REQUEST) {
  		addr_to_sbp2_pointer(agent->orb_offset, ptr);
  		rcode = RCODE_COMPLETE;
  	} else {
  		rcode = RCODE_TYPE_ERROR;
  	}
  
  out:
  	fw_send_response(card, request, rcode);
  }
  
  static struct sbp_management_agent *sbp_management_agent_register(
  		struct sbp_tport *tport)
  {
  	int ret;
  	struct sbp_management_agent *agent;
  
  	agent = kmalloc(sizeof(*agent), GFP_KERNEL);
  	if (!agent)
  		return ERR_PTR(-ENOMEM);
  
  	spin_lock_init(&agent->lock);
  	agent->tport = tport;
  	agent->handler.length = 0x08;
  	agent->handler.address_callback = sbp_mgt_agent_rw;
  	agent->handler.callback_data = agent;
  	agent->state = MANAGEMENT_AGENT_STATE_IDLE;
  	INIT_WORK(&agent->work, sbp_mgt_agent_process);
  	agent->orb_offset = 0;
  	agent->request = NULL;
  
  	ret = fw_core_add_address_handler(&agent->handler,
  			&sbp_register_region);
  	if (ret < 0) {
  		kfree(agent);
  		return ERR_PTR(ret);
  	}
  
  	return agent;
  }
  
  static void sbp_management_agent_unregister(struct sbp_management_agent *agent)
  {
  	fw_core_remove_address_handler(&agent->handler);
  	cancel_work_sync(&agent->work);
  	kfree(agent);
  }
  
  static int sbp_check_true(struct se_portal_group *se_tpg)
  {
  	return 1;
  }
  
  static int sbp_check_false(struct se_portal_group *se_tpg)
  {
  	return 0;
  }
  
  static char *sbp_get_fabric_name(void)
  {
  	return "sbp";
  }
  
  static char *sbp_get_fabric_wwn(struct se_portal_group *se_tpg)
  {
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  
  	return &tport->tport_name[0];
  }
  
  static u16 sbp_get_tag(struct se_portal_group *se_tpg)
  {
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	return tpg->tport_tpgt;
  }
a511ce339   Chris Boot   sbp-target: Initi...
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
  static u32 sbp_tpg_get_inst_index(struct se_portal_group *se_tpg)
  {
  	return 1;
  }
  
  static void sbp_release_cmd(struct se_cmd *se_cmd)
  {
  	struct sbp_target_request *req = container_of(se_cmd,
  			struct sbp_target_request, se_cmd);
  
  	sbp_free_request(req);
  }
a511ce339   Chris Boot   sbp-target: Initi...
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
  static u32 sbp_sess_get_index(struct se_session *se_sess)
  {
  	return 0;
  }
  
  static int sbp_write_pending(struct se_cmd *se_cmd)
  {
  	struct sbp_target_request *req = container_of(se_cmd,
  			struct sbp_target_request, se_cmd);
  	int ret;
  
  	ret = sbp_rw_data(req);
  	if (ret) {
  		req->status.status |= cpu_to_be32(
  			STATUS_BLOCK_RESP(
  				STATUS_RESP_TRANSPORT_FAILURE) |
  			STATUS_BLOCK_DEAD(0) |
  			STATUS_BLOCK_LEN(1) |
  			STATUS_BLOCK_SBP_STATUS(
  				SBP_STATUS_UNSPECIFIED_ERROR));
  		sbp_send_status(req);
  		return ret;
  	}
70baf0ab3   Christoph Hellwig   target: remove tr...
1791
  	target_execute_cmd(se_cmd);
a511ce339   Chris Boot   sbp-target: Initi...
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
  	return 0;
  }
  
  static int sbp_write_pending_status(struct se_cmd *se_cmd)
  {
  	return 0;
  }
  
  static void sbp_set_default_node_attrs(struct se_node_acl *nacl)
  {
  	return;
  }
a511ce339   Chris Boot   sbp-target: Initi...
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
  static int sbp_get_cmd_state(struct se_cmd *se_cmd)
  {
  	return 0;
  }
  
  static int sbp_queue_data_in(struct se_cmd *se_cmd)
  {
  	struct sbp_target_request *req = container_of(se_cmd,
  			struct sbp_target_request, se_cmd);
  	int ret;
  
  	ret = sbp_rw_data(req);
  	if (ret) {
  		req->status.status |= cpu_to_be32(
  			STATUS_BLOCK_RESP(STATUS_RESP_TRANSPORT_FAILURE) |
  			STATUS_BLOCK_DEAD(0) |
  			STATUS_BLOCK_LEN(1) |
  			STATUS_BLOCK_SBP_STATUS(SBP_STATUS_UNSPECIFIED_ERROR));
  		sbp_send_status(req);
  		return ret;
  	}
  
  	return sbp_send_sense(req);
  }
  
  /*
   * Called after command (no data transfer) or after the write (to device)
   * operation is completed
   */
  static int sbp_queue_status(struct se_cmd *se_cmd)
  {
  	struct sbp_target_request *req = container_of(se_cmd,
  			struct sbp_target_request, se_cmd);
  
  	return sbp_send_sense(req);
  }
b79fafac7   Joern Engel   target: make queu...
1840
  static void sbp_queue_tm_rsp(struct se_cmd *se_cmd)
a511ce339   Chris Boot   sbp-target: Initi...
1841
  {
a511ce339   Chris Boot   sbp-target: Initi...
1842
  }
131e6abc6   Nicholas Bellinger   target: Add TFO->...
1843
1844
1845
1846
  static void sbp_aborted_task(struct se_cmd *se_cmd)
  {
  	return;
  }
a511ce339   Chris Boot   sbp-target: Initi...
1847
1848
1849
1850
  static int sbp_check_stop_free(struct se_cmd *se_cmd)
  {
  	struct sbp_target_request *req = container_of(se_cmd,
  			struct sbp_target_request, se_cmd);
5f27edad9   Nicholas Bellinger   sbp-target: Conve...
1851
  	return transport_generic_free_cmd(&req->se_cmd, 0);
a511ce339   Chris Boot   sbp-target: Initi...
1852
  }
a511ce339   Chris Boot   sbp-target: Initi...
1853
1854
  static int sbp_count_se_tpg_luns(struct se_portal_group *tpg)
  {
6bb826121   Nicholas Bellinger   target: Convert s...
1855
1856
  	struct se_lun *lun;
  	int count = 0;
a511ce339   Chris Boot   sbp-target: Initi...
1857

6bb826121   Nicholas Bellinger   target: Convert s...
1858
1859
  	rcu_read_lock();
  	hlist_for_each_entry_rcu(lun, &tpg->tpg_lun_hlist, link)
a511ce339   Chris Boot   sbp-target: Initi...
1860
  		count++;
6bb826121   Nicholas Bellinger   target: Convert s...
1861
  	rcu_read_unlock();
a511ce339   Chris Boot   sbp-target: Initi...
1862
1863
1864
1865
1866
1867
  
  	return count;
  }
  
  static int sbp_update_unit_directory(struct sbp_tport *tport)
  {
6bb826121   Nicholas Bellinger   target: Convert s...
1868
1869
  	struct se_lun *lun;
  	int num_luns, num_entries, idx = 0, mgt_agt_addr, ret;
a511ce339   Chris Boot   sbp-target: Initi...
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
  	u32 *data;
  
  	if (tport->unit_directory.data) {
  		fw_core_remove_descriptor(&tport->unit_directory);
  		kfree(tport->unit_directory.data);
  		tport->unit_directory.data = NULL;
  	}
  
  	if (!tport->enable || !tport->tpg)
  		return 0;
  
  	num_luns = sbp_count_se_tpg_luns(&tport->tpg->se_tpg);
  
  	/*
  	 * Number of entries in the final unit directory:
  	 *  - all of those in the template
  	 *  - management_agent
  	 *  - unit_characteristics
  	 *  - reconnect_timeout
  	 *  - unit unique ID
  	 *  - one for each LUN
  	 *
  	 *  MUST NOT include leaf or sub-directory entries
  	 */
  	num_entries = ARRAY_SIZE(sbp_unit_directory_template) + 4 + num_luns;
  
  	if (tport->directory_id != -1)
  		num_entries++;
  
  	/* allocate num_entries + 4 for the header and unique ID leaf */
  	data = kcalloc((num_entries + 4), sizeof(u32), GFP_KERNEL);
  	if (!data)
  		return -ENOMEM;
  
  	/* directory_length */
  	data[idx++] = num_entries << 16;
  
  	/* directory_id */
  	if (tport->directory_id != -1)
  		data[idx++] = (CSR_DIRECTORY_ID << 24) | tport->directory_id;
  
  	/* unit directory template */
  	memcpy(&data[idx], sbp_unit_directory_template,
  			sizeof(sbp_unit_directory_template));
  	idx += ARRAY_SIZE(sbp_unit_directory_template);
  
  	/* management_agent */
  	mgt_agt_addr = (tport->mgt_agt->handler.offset - CSR_REGISTER_BASE) / 4;
  	data[idx++] = 0x54000000 | (mgt_agt_addr & 0x00ffffff);
  
  	/* unit_characteristics */
  	data[idx++] = 0x3a000000 |
  		(((tport->mgt_orb_timeout * 2) << 8) & 0xff00) |
  		SBP_ORB_FETCH_SIZE;
  
  	/* reconnect_timeout */
  	data[idx++] = 0x3d000000 | (tport->max_reconnect_timeout & 0xffff);
  
  	/* unit unique ID (leaf is just after LUNs) */
  	data[idx++] = 0x8d000000 | (num_luns + 1);
6bb826121   Nicholas Bellinger   target: Convert s...
1930
1931
  	rcu_read_lock();
  	hlist_for_each_entry_rcu(lun, &tport->tpg->se_tpg.tpg_lun_hlist, link) {
a511ce339   Chris Boot   sbp-target: Initi...
1932
1933
  		struct se_device *dev;
  		int type;
4cc987eaf   Nicholas Bellinger   target: Drop lun_...
1934
1935
1936
1937
1938
  		/*
  		 * rcu_dereference_raw protected by se_lun->lun_group symlink
  		 * reference to se_device->dev_group.
  		 */
  		dev = rcu_dereference_raw(lun->lun_se_dev);
a511ce339   Chris Boot   sbp-target: Initi...
1939
1940
1941
1942
1943
  		type = dev->transport->get_device_type(dev);
  
  		/* logical_unit_number */
  		data[idx++] = 0x14000000 |
  			((type << 16) & 0x1f0000) |
6bb826121   Nicholas Bellinger   target: Convert s...
1944
  			(lun->unpacked_lun & 0xffff);
a511ce339   Chris Boot   sbp-target: Initi...
1945
  	}
6bb826121   Nicholas Bellinger   target: Convert s...
1946
  	rcu_read_unlock();
a511ce339   Chris Boot   sbp-target: Initi...
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
  
  	/* unit unique ID leaf */
  	data[idx++] = 2 << 16;
  	data[idx++] = tport->guid >> 32;
  	data[idx++] = tport->guid;
  
  	tport->unit_directory.length = idx;
  	tport->unit_directory.key = (CSR_DIRECTORY | CSR_UNIT) << 24;
  	tport->unit_directory.data = data;
  
  	ret = fw_core_add_descriptor(&tport->unit_directory);
  	if (ret < 0) {
  		kfree(tport->unit_directory.data);
  		tport->unit_directory.data = NULL;
  	}
  
  	return ret;
  }
343d475d6   Andy Grover   target/sbp: Remov...
1965
  static ssize_t sbp_parse_wwn(const char *name, u64 *wwn)
a511ce339   Chris Boot   sbp-target: Initi...
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
  {
  	const char *cp;
  	char c, nibble;
  	int pos = 0, err;
  
  	*wwn = 0;
  	for (cp = name; cp < &name[SBP_NAMELEN - 1]; cp++) {
  		c = *cp;
  		if (c == '
  ' && cp[1] == '\0')
  			continue;
  		if (c == '\0') {
  			err = 2;
  			if (pos != 16)
  				goto fail;
  			return cp - name;
  		}
  		err = 3;
  		if (isdigit(c))
  			nibble = c - '0';
343d475d6   Andy Grover   target/sbp: Remov...
1986
  		else if (isxdigit(c))
a511ce339   Chris Boot   sbp-target: Initi...
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
  			nibble = tolower(c) - 'a' + 10;
  		else
  			goto fail;
  		*wwn = (*wwn << 4) | nibble;
  		pos++;
  	}
  	err = 4;
  fail:
  	printk(KERN_INFO "err %u len %zu pos %u
  ",
  			err, cp - name, pos);
  	return -1;
  }
  
  static ssize_t sbp_format_wwn(char *buf, size_t len, u64 wwn)
  {
  	return snprintf(buf, len, "%016llx", wwn);
  }
c7d6a8039   Christoph Hellwig   target: refactor ...
2005
  static int sbp_init_nodeacl(struct se_node_acl *se_nacl, const char *name)
a511ce339   Chris Boot   sbp-target: Initi...
2006
  {
a511ce339   Chris Boot   sbp-target: Initi...
2007
  	u64 guid = 0;
a511ce339   Chris Boot   sbp-target: Initi...
2008

343d475d6   Andy Grover   target/sbp: Remov...
2009
  	if (sbp_parse_wwn(name, &guid) < 0)
c7d6a8039   Christoph Hellwig   target: refactor ...
2010
2011
  		return -EINVAL;
  	return 0;
a511ce339   Chris Boot   sbp-target: Initi...
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
  }
  
  static int sbp_post_link_lun(
  		struct se_portal_group *se_tpg,
  		struct se_lun *se_lun)
  {
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  
  	return sbp_update_unit_directory(tpg->tport);
  }
  
  static void sbp_pre_unlink_lun(
  		struct se_portal_group *se_tpg,
  		struct se_lun *se_lun)
  {
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  	int ret;
  
  	if (sbp_count_se_tpg_luns(&tpg->se_tpg) == 0)
  		tport->enable = 0;
  
  	ret = sbp_update_unit_directory(tport);
  	if (ret < 0)
  		pr_err("unlink LUN: failed to update unit directory
  ");
  }
aa090eabc   Bart Van Assche   scsi: target: Rem...
2039
2040
  static struct se_portal_group *sbp_make_tpg(struct se_wwn *wwn,
  					    const char *name)
a511ce339   Chris Boot   sbp-target: Initi...
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
  {
  	struct sbp_tport *tport =
  		container_of(wwn, struct sbp_tport, tport_wwn);
  
  	struct sbp_tpg *tpg;
  	unsigned long tpgt;
  	int ret;
  
  	if (strstr(name, "tpgt_") != name)
  		return ERR_PTR(-EINVAL);
  	if (kstrtoul(name + 5, 10, &tpgt) || tpgt > UINT_MAX)
  		return ERR_PTR(-EINVAL);
  
  	if (tport->tpg) {
  		pr_err("Only one TPG per Unit is possible.
  ");
  		return ERR_PTR(-EBUSY);
  	}
  
  	tpg = kzalloc(sizeof(*tpg), GFP_KERNEL);
e0f3d4c23   Markus Elfring   sbp-target: Delet...
2061
  	if (!tpg)
a511ce339   Chris Boot   sbp-target: Initi...
2062
  		return ERR_PTR(-ENOMEM);
a511ce339   Chris Boot   sbp-target: Initi...
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
  
  	tpg->tport = tport;
  	tpg->tport_tpgt = tpgt;
  	tport->tpg = tpg;
  
  	/* default attribute values */
  	tport->enable = 0;
  	tport->directory_id = -1;
  	tport->mgt_orb_timeout = 15;
  	tport->max_reconnect_timeout = 5;
  	tport->max_logins_per_lun = 1;
  
  	tport->mgt_agt = sbp_management_agent_register(tport);
  	if (IS_ERR(tport->mgt_agt)) {
  		ret = PTR_ERR(tport->mgt_agt);
e1fe2060d   Chris Boot   sbp-target: fix e...
2078
  		goto out_free_tpg;
a511ce339   Chris Boot   sbp-target: Initi...
2079
  	}
bc0c94b14   Nicholas Bellinger   target: Drop unne...
2080
  	ret = core_tpg_register(wwn, &tpg->se_tpg, SCSI_PROTOCOL_SBP);
e1fe2060d   Chris Boot   sbp-target: fix e...
2081
2082
  	if (ret < 0)
  		goto out_unreg_mgt_agt;
a511ce339   Chris Boot   sbp-target: Initi...
2083
2084
  
  	return &tpg->se_tpg;
e1fe2060d   Chris Boot   sbp-target: fix e...
2085
2086
2087
2088
2089
2090
2091
  
  out_unreg_mgt_agt:
  	sbp_management_agent_unregister(tport->mgt_agt);
  out_free_tpg:
  	tport->tpg = NULL;
  	kfree(tpg);
  	return ERR_PTR(ret);
a511ce339   Chris Boot   sbp-target: Initi...
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
  }
  
  static void sbp_drop_tpg(struct se_portal_group *se_tpg)
  {
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  
  	core_tpg_deregister(se_tpg);
  	sbp_management_agent_unregister(tport->mgt_agt);
  	tport->tpg = NULL;
  	kfree(tpg);
  }
  
  static struct se_wwn *sbp_make_tport(
  		struct target_fabric_configfs *tf,
  		struct config_group *group,
  		const char *name)
  {
  	struct sbp_tport *tport;
  	u64 guid = 0;
343d475d6   Andy Grover   target/sbp: Remov...
2112
  	if (sbp_parse_wwn(name, &guid) < 0)
a511ce339   Chris Boot   sbp-target: Initi...
2113
2114
2115
  		return ERR_PTR(-EINVAL);
  
  	tport = kzalloc(sizeof(*tport), GFP_KERNEL);
e0f3d4c23   Markus Elfring   sbp-target: Delet...
2116
  	if (!tport)
a511ce339   Chris Boot   sbp-target: Initi...
2117
  		return ERR_PTR(-ENOMEM);
a511ce339   Chris Boot   sbp-target: Initi...
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
  
  	tport->guid = guid;
  	sbp_format_wwn(tport->tport_name, SBP_NAMELEN, guid);
  
  	return &tport->tport_wwn;
  }
  
  static void sbp_drop_tport(struct se_wwn *wwn)
  {
  	struct sbp_tport *tport =
  		container_of(wwn, struct sbp_tport, tport_wwn);
  
  	kfree(tport);
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2132
  static ssize_t sbp_wwn_version_show(struct config_item *item, char *page)
a511ce339   Chris Boot   sbp-target: Initi...
2133
2134
2135
2136
  {
  	return sprintf(page, "FireWire SBP fabric module %s
  ", SBP_VERSION);
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2137
  CONFIGFS_ATTR_RO(sbp_wwn_, version);
a511ce339   Chris Boot   sbp-target: Initi...
2138
2139
  
  static struct configfs_attribute *sbp_wwn_attrs[] = {
2eafd7293   Christoph Hellwig   target: use per-a...
2140
  	&sbp_wwn_attr_version,
a511ce339   Chris Boot   sbp-target: Initi...
2141
2142
  	NULL,
  };
2eafd7293   Christoph Hellwig   target: use per-a...
2143
  static ssize_t sbp_tpg_directory_id_show(struct config_item *item, char *page)
a511ce339   Chris Boot   sbp-target: Initi...
2144
  {
2eafd7293   Christoph Hellwig   target: use per-a...
2145
  	struct se_portal_group *se_tpg = to_tpg(item);
a511ce339   Chris Boot   sbp-target: Initi...
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  
  	if (tport->directory_id == -1)
  		return sprintf(page, "implicit
  ");
  	else
  		return sprintf(page, "%06x
  ", tport->directory_id);
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2156
2157
  static ssize_t sbp_tpg_directory_id_store(struct config_item *item,
  		const char *page, size_t count)
a511ce339   Chris Boot   sbp-target: Initi...
2158
  {
2eafd7293   Christoph Hellwig   target: use per-a...
2159
  	struct se_portal_group *se_tpg = to_tpg(item);
a511ce339   Chris Boot   sbp-target: Initi...
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  	unsigned long val;
  
  	if (tport->enable) {
  		pr_err("Cannot change the directory_id on an active target.
  ");
  		return -EBUSY;
  	}
  
  	if (strstr(page, "implicit") == page) {
  		tport->directory_id = -1;
  	} else {
  		if (kstrtoul(page, 16, &val) < 0)
  			return -EINVAL;
  		if (val > 0xffffff)
  			return -EINVAL;
  
  		tport->directory_id = val;
  	}
  
  	return count;
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2183
  static ssize_t sbp_tpg_enable_show(struct config_item *item, char *page)
a511ce339   Chris Boot   sbp-target: Initi...
2184
  {
2eafd7293   Christoph Hellwig   target: use per-a...
2185
  	struct se_portal_group *se_tpg = to_tpg(item);
a511ce339   Chris Boot   sbp-target: Initi...
2186
2187
2188
2189
2190
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  	return sprintf(page, "%d
  ", tport->enable);
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2191
2192
  static ssize_t sbp_tpg_enable_store(struct config_item *item,
  		const char *page, size_t count)
a511ce339   Chris Boot   sbp-target: Initi...
2193
  {
2eafd7293   Christoph Hellwig   target: use per-a...
2194
  	struct se_portal_group *se_tpg = to_tpg(item);
a511ce339   Chris Boot   sbp-target: Initi...
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  	unsigned long val;
  	int ret;
  
  	if (kstrtoul(page, 0, &val) < 0)
  		return -EINVAL;
  	if ((val != 0) && (val != 1))
  		return -EINVAL;
  
  	if (tport->enable == val)
  		return count;
  
  	if (val) {
  		if (sbp_count_se_tpg_luns(&tpg->se_tpg) == 0) {
  			pr_err("Cannot enable a target with no LUNs!
  ");
  			return -EINVAL;
  		}
  	} else {
  		/* XXX: force-shutdown sessions instead? */
  		spin_lock_bh(&se_tpg->session_lock);
  		if (!list_empty(&se_tpg->tpg_sess_list)) {
  			spin_unlock_bh(&se_tpg->session_lock);
  			return -EBUSY;
  		}
  		spin_unlock_bh(&se_tpg->session_lock);
  	}
  
  	tport->enable = val;
  
  	ret = sbp_update_unit_directory(tport);
  	if (ret < 0) {
  		pr_err("Could not update Config ROM
  ");
  		return ret;
  	}
  
  	return count;
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2235
2236
  CONFIGFS_ATTR(sbp_tpg_, directory_id);
  CONFIGFS_ATTR(sbp_tpg_, enable);
a511ce339   Chris Boot   sbp-target: Initi...
2237
2238
  
  static struct configfs_attribute *sbp_tpg_base_attrs[] = {
2eafd7293   Christoph Hellwig   target: use per-a...
2239
2240
  	&sbp_tpg_attr_directory_id,
  	&sbp_tpg_attr_enable,
a511ce339   Chris Boot   sbp-target: Initi...
2241
2242
  	NULL,
  };
2eafd7293   Christoph Hellwig   target: use per-a...
2243
  static ssize_t sbp_tpg_attrib_mgt_orb_timeout_show(struct config_item *item,
a511ce339   Chris Boot   sbp-target: Initi...
2244
2245
  		char *page)
  {
2eafd7293   Christoph Hellwig   target: use per-a...
2246
  	struct se_portal_group *se_tpg = attrib_to_tpg(item);
a511ce339   Chris Boot   sbp-target: Initi...
2247
2248
2249
2250
2251
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  	return sprintf(page, "%d
  ", tport->mgt_orb_timeout);
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2252
2253
  static ssize_t sbp_tpg_attrib_mgt_orb_timeout_store(struct config_item *item,
  		const char *page, size_t count)
a511ce339   Chris Boot   sbp-target: Initi...
2254
  {
2eafd7293   Christoph Hellwig   target: use per-a...
2255
  	struct se_portal_group *se_tpg = attrib_to_tpg(item);
a511ce339   Chris Boot   sbp-target: Initi...
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  	unsigned long val;
  	int ret;
  
  	if (kstrtoul(page, 0, &val) < 0)
  		return -EINVAL;
  	if ((val < 1) || (val > 127))
  		return -EINVAL;
  
  	if (tport->mgt_orb_timeout == val)
  		return count;
  
  	tport->mgt_orb_timeout = val;
  
  	ret = sbp_update_unit_directory(tport);
  	if (ret < 0)
  		return ret;
  
  	return count;
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2277
  static ssize_t sbp_tpg_attrib_max_reconnect_timeout_show(struct config_item *item,
a511ce339   Chris Boot   sbp-target: Initi...
2278
2279
  		char *page)
  {
2eafd7293   Christoph Hellwig   target: use per-a...
2280
  	struct se_portal_group *se_tpg = attrib_to_tpg(item);
a511ce339   Chris Boot   sbp-target: Initi...
2281
2282
2283
2284
2285
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  	return sprintf(page, "%d
  ", tport->max_reconnect_timeout);
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2286
2287
  static ssize_t sbp_tpg_attrib_max_reconnect_timeout_store(struct config_item *item,
  		const char *page, size_t count)
a511ce339   Chris Boot   sbp-target: Initi...
2288
  {
2eafd7293   Christoph Hellwig   target: use per-a...
2289
  	struct se_portal_group *se_tpg = attrib_to_tpg(item);
a511ce339   Chris Boot   sbp-target: Initi...
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  	unsigned long val;
  	int ret;
  
  	if (kstrtoul(page, 0, &val) < 0)
  		return -EINVAL;
  	if ((val < 1) || (val > 32767))
  		return -EINVAL;
  
  	if (tport->max_reconnect_timeout == val)
  		return count;
  
  	tport->max_reconnect_timeout = val;
  
  	ret = sbp_update_unit_directory(tport);
  	if (ret < 0)
  		return ret;
  
  	return count;
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2311
  static ssize_t sbp_tpg_attrib_max_logins_per_lun_show(struct config_item *item,
a511ce339   Chris Boot   sbp-target: Initi...
2312
2313
  		char *page)
  {
2eafd7293   Christoph Hellwig   target: use per-a...
2314
  	struct se_portal_group *se_tpg = attrib_to_tpg(item);
a511ce339   Chris Boot   sbp-target: Initi...
2315
2316
2317
2318
2319
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  	return sprintf(page, "%d
  ", tport->max_logins_per_lun);
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2320
2321
  static ssize_t sbp_tpg_attrib_max_logins_per_lun_store(struct config_item *item,
  		const char *page, size_t count)
a511ce339   Chris Boot   sbp-target: Initi...
2322
  {
2eafd7293   Christoph Hellwig   target: use per-a...
2323
  	struct se_portal_group *se_tpg = attrib_to_tpg(item);
a511ce339   Chris Boot   sbp-target: Initi...
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
  	struct sbp_tpg *tpg = container_of(se_tpg, struct sbp_tpg, se_tpg);
  	struct sbp_tport *tport = tpg->tport;
  	unsigned long val;
  
  	if (kstrtoul(page, 0, &val) < 0)
  		return -EINVAL;
  	if ((val < 1) || (val > 127))
  		return -EINVAL;
  
  	/* XXX: also check against current count? */
  
  	tport->max_logins_per_lun = val;
  
  	return count;
  }
2eafd7293   Christoph Hellwig   target: use per-a...
2339
2340
2341
  CONFIGFS_ATTR(sbp_tpg_attrib_, mgt_orb_timeout);
  CONFIGFS_ATTR(sbp_tpg_attrib_, max_reconnect_timeout);
  CONFIGFS_ATTR(sbp_tpg_attrib_, max_logins_per_lun);
a511ce339   Chris Boot   sbp-target: Initi...
2342
2343
  
  static struct configfs_attribute *sbp_tpg_attrib_attrs[] = {
2eafd7293   Christoph Hellwig   target: use per-a...
2344
2345
2346
  	&sbp_tpg_attrib_attr_mgt_orb_timeout,
  	&sbp_tpg_attrib_attr_max_reconnect_timeout,
  	&sbp_tpg_attrib_attr_max_logins_per_lun,
a511ce339   Chris Boot   sbp-target: Initi...
2347
2348
  	NULL,
  };
9ac8928e6   Christoph Hellwig   target: simplify ...
2349
2350
2351
  static const struct target_core_fabric_ops sbp_ops = {
  	.module				= THIS_MODULE,
  	.name				= "sbp",
a511ce339   Chris Boot   sbp-target: Initi...
2352
  	.get_fabric_name		= sbp_get_fabric_name,
a511ce339   Chris Boot   sbp-target: Initi...
2353
2354
  	.tpg_get_wwn			= sbp_get_fabric_wwn,
  	.tpg_get_tag			= sbp_get_tag,
a511ce339   Chris Boot   sbp-target: Initi...
2355
2356
2357
2358
  	.tpg_check_demo_mode		= sbp_check_true,
  	.tpg_check_demo_mode_cache	= sbp_check_true,
  	.tpg_check_demo_mode_write_protect = sbp_check_false,
  	.tpg_check_prod_mode_write_protect = sbp_check_false,
a511ce339   Chris Boot   sbp-target: Initi...
2359
2360
  	.tpg_get_inst_index		= sbp_tpg_get_inst_index,
  	.release_cmd			= sbp_release_cmd,
a511ce339   Chris Boot   sbp-target: Initi...
2361
2362
2363
2364
  	.sess_get_index			= sbp_sess_get_index,
  	.write_pending			= sbp_write_pending,
  	.write_pending_status		= sbp_write_pending_status,
  	.set_default_node_attributes	= sbp_set_default_node_attrs,
a511ce339   Chris Boot   sbp-target: Initi...
2365
2366
2367
2368
  	.get_cmd_state			= sbp_get_cmd_state,
  	.queue_data_in			= sbp_queue_data_in,
  	.queue_status			= sbp_queue_status,
  	.queue_tm_rsp			= sbp_queue_tm_rsp,
131e6abc6   Nicholas Bellinger   target: Add TFO->...
2369
  	.aborted_task			= sbp_aborted_task,
a511ce339   Chris Boot   sbp-target: Initi...
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
  	.check_stop_free		= sbp_check_stop_free,
  
  	.fabric_make_wwn		= sbp_make_tport,
  	.fabric_drop_wwn		= sbp_drop_tport,
  	.fabric_make_tpg		= sbp_make_tpg,
  	.fabric_drop_tpg		= sbp_drop_tpg,
  	.fabric_post_link		= sbp_post_link_lun,
  	.fabric_pre_unlink		= sbp_pre_unlink_lun,
  	.fabric_make_np			= NULL,
  	.fabric_drop_np			= NULL,
c7d6a8039   Christoph Hellwig   target: refactor ...
2380
  	.fabric_init_nodeacl		= sbp_init_nodeacl,
a511ce339   Chris Boot   sbp-target: Initi...
2381

9ac8928e6   Christoph Hellwig   target: simplify ...
2382
2383
2384
  	.tfc_wwn_attrs			= sbp_wwn_attrs,
  	.tfc_tpg_base_attrs		= sbp_tpg_base_attrs,
  	.tfc_tpg_attrib_attrs		= sbp_tpg_attrib_attrs,
a511ce339   Chris Boot   sbp-target: Initi...
2385
2386
2387
2388
  };
  
  static int __init sbp_init(void)
  {
9ac8928e6   Christoph Hellwig   target: simplify ...
2389
  	return target_register_template(&sbp_ops);
a511ce339   Chris Boot   sbp-target: Initi...
2390
  };
63b91d5a4   Asias He   target: Add __exi...
2391
  static void __exit sbp_exit(void)
a511ce339   Chris Boot   sbp-target: Initi...
2392
  {
9ac8928e6   Christoph Hellwig   target: simplify ...
2393
  	target_unregister_template(&sbp_ops);
a511ce339   Chris Boot   sbp-target: Initi...
2394
2395
2396
2397
2398
2399
  };
  
  MODULE_DESCRIPTION("FireWire SBP fabric driver");
  MODULE_LICENSE("GPL");
  module_init(sbp_init);
  module_exit(sbp_exit);