1/*
2 * Copyright 2010, Andreas F��rber <andreas.faerber@web.de>
3 * All rights reserved. Distributed under the terms of the MIT License.
4 */
5#ifndef BOOT_NET_ISCSITARGET_H
6#define BOOT_NET_ISCSITARGET_H
7
8
9#include <SupportDefs.h>
10#include <util/kernel_cpp.h>
11
12#include <boot/platform.h>
13#include <boot/stdio.h>
14#include <boot/vfs.h>
15#include <boot/net/NetDefs.h>
16
17
18#define ISCSI_PORT 3260
19
20class TCPSocket;
21
22class iSCSISession;
23
24struct iscsi_basic_header_segment;
25
26#define kTcpTimeout	10000000ULL
27
28
29class iSCSIConnection {
30public:
31	iSCSIConnection();
32	virtual ~iSCSIConnection();
33
34	status_t Init(iSCSISession* session);
35	status_t Open(ip_addr_t address, uint16 port);
36	status_t Login(const char* targetName = NULL, char** targetAlias = NULL);
37	status_t Logout(bool closeSession = false);
38	status_t GetText(const char* request, size_t requestLength, char** response, size_t* responseLength);
39	status_t SendCommand(const void* command, size_t commandSize,
40		bool r, bool w, uint32 expectedDataTransferLength,
41		void* response, uint32 responseOffset, size_t responseLength);
42
43	bool Active() const { return fConnected; }
44
45private:
46	status_t	_ReadResponse(iscsi_basic_header_segment* buffer, bigtime_t timeout = kTcpTimeout);
47	status_t	_Read(void* buffer, size_t bufferSize, bigtime_t timeout = kTcpTimeout);
48
49	iSCSISession* fSession;
50	TCPSocket* fSocket;
51	bool fConnected;
52	uint16 fConnectionID;
53	uint32 fStatusSequenceNumber;
54};
55
56class iSCSISession {
57public:
58	iSCSISession(const char* targetName = NULL);
59	virtual ~iSCSISession();
60
61	status_t Init(ip_addr_t address, uint16 port, char** targetAlias = NULL);
62	status_t Close();
63
64	uint32 CommandSequenceNumber() const { return fCommandSequenceNumber; }
65	uint32 NextCommandSequenceNumber() { return fCommandSequenceNumber++; }
66	iSCSIConnection* Connection() const { return fConnection; }
67
68	bool Active() { return fConnection != NULL && fConnection->Active(); }
69
70private:
71	bool fDiscovery;
72	const char* fTargetName;
73	uint32 fCommandSequenceNumber;
74	iSCSIConnection* fConnection;
75		// XXX should allow for multiple
76};
77
78class iSCSITarget : public Node {
79public:
80	iSCSITarget();
81	virtual ~iSCSITarget();
82	status_t Init(ip_addr_t address, uint16 port, const char* targetName);
83
84	virtual ssize_t ReadAt(void* cookie, off_t pos, void* buffer,
85		size_t bufferSize);
86	virtual ssize_t WriteAt(void* cookie, off_t pos, const void* buffer,
87		size_t bufferSize);
88
89	virtual status_t GetName(char* buffer, size_t bufferSize) const;
90	virtual off_t Size() const;
91
92	static bool DiscoverTargets(ip_addr_t address, uint16 port,
93		NodeList* devicesList);
94	static bool _AddDevice(ip_addr_t address, uint16 port,
95		const char* targetName, NodeList* devicesList);
96
97private:
98	status_t _GetSize();
99
100	iSCSISession* fSession;
101	char* fTargetName;
102	char* fTargetAlias;
103	uint32 fLastLBA;
104	uint32 fBlockSize;
105};
106
107
108#endif
109