libfc.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Maintained at www.Open-FCoE.org
  18. */
  19. #ifndef _LIBFC_H_
  20. #define _LIBFC_H_
  21. #include <linux/timer.h>
  22. #include <linux/if.h>
  23. #include <linux/percpu.h>
  24. #include <scsi/scsi_transport.h>
  25. #include <scsi/scsi_transport_fc.h>
  26. #include <scsi/scsi_bsg_fc.h>
  27. #include <scsi/fc/fc_fcp.h>
  28. #include <scsi/fc/fc_ns.h>
  29. #include <scsi/fc/fc_els.h>
  30. #include <scsi/fc/fc_gs.h>
  31. #include <scsi/fc_frame.h>
  32. /*
  33. * libfc error codes
  34. */
  35. #define FC_NO_ERR 0 /* no error */
  36. #define FC_EX_TIMEOUT 1 /* Exchange timeout */
  37. #define FC_EX_CLOSED 2 /* Exchange closed */
  38. /* some helpful macros */
  39. #define ntohll(x) be64_to_cpu(x)
  40. #define htonll(x) cpu_to_be64(x)
  41. #define ntoh24(p) (((p)[0] << 16) | ((p)[1] << 8) | ((p)[2]))
  42. #define hton24(p, v) do { \
  43. p[0] = (((v) >> 16) & 0xFF); \
  44. p[1] = (((v) >> 8) & 0xFF); \
  45. p[2] = ((v) & 0xFF); \
  46. } while (0)
  47. /**
  48. * enum fc_lport_state - Local port states
  49. * @LPORT_ST_DISABLED: Disabled
  50. * @LPORT_ST_FLOGI: Fabric login (FLOGI) sent
  51. * @LPORT_ST_DNS: Waiting for name server remote port to become ready
  52. * @LPORT_ST_RPN_ID: Register port name by ID (RPN_ID) sent
  53. * @LPORT_ST_RFT_ID: Register Fibre Channel types by ID (RFT_ID) sent
  54. * @LPORT_ST_SCR: State Change Register (SCR) sent
  55. * @LPORT_ST_READY: Ready for use
  56. * @LPORT_ST_LOGO: Local port logout (LOGO) sent
  57. * @LPORT_ST_RESET: Local port reset
  58. */
  59. enum fc_lport_state {
  60. LPORT_ST_DISABLED = 0,
  61. LPORT_ST_FLOGI,
  62. LPORT_ST_DNS,
  63. LPORT_ST_RNN_ID,
  64. LPORT_ST_RSNN_NN,
  65. LPORT_ST_RSPN_ID,
  66. LPORT_ST_RFT_ID,
  67. LPORT_ST_SCR,
  68. LPORT_ST_READY,
  69. LPORT_ST_LOGO,
  70. LPORT_ST_RESET
  71. };
  72. enum fc_disc_event {
  73. DISC_EV_NONE = 0,
  74. DISC_EV_SUCCESS,
  75. DISC_EV_FAILED
  76. };
  77. /**
  78. * enum fc_rport_state - Remote port states
  79. * @RPORT_ST_INIT: Initialized
  80. * @RPORT_ST_PLOGI: Waiting for PLOGI completion
  81. * @RPORT_ST_PRLI: Waiting for PRLI completion
  82. * @RPORT_ST_RTV: Waiting for RTV completion
  83. * @RPORT_ST_READY: Ready for use
  84. * @RPORT_ST_LOGO: Remote port logout (LOGO) sent
  85. * @RPORT_ST_ADISC: Discover Address sent
  86. * @RPORT_ST_DELETE: Remote port being deleted
  87. * @RPORT_ST_RESTART: Remote port being deleted and will restart
  88. */
  89. enum fc_rport_state {
  90. RPORT_ST_INIT,
  91. RPORT_ST_PLOGI,
  92. RPORT_ST_PRLI,
  93. RPORT_ST_RTV,
  94. RPORT_ST_READY,
  95. RPORT_ST_LOGO,
  96. RPORT_ST_ADISC,
  97. RPORT_ST_DELETE,
  98. RPORT_ST_RESTART,
  99. };
  100. /**
  101. * struct fc_disc_port - temporary discovery port to hold rport identifiers
  102. * @lp: Fibre Channel host port instance
  103. * @peers: Node for list management during discovery and RSCN processing
  104. * @rport_work: Work struct for starting the rport state machine
  105. * @port_id: Port ID of the discovered port
  106. */
  107. struct fc_disc_port {
  108. struct fc_lport *lp;
  109. struct list_head peers;
  110. struct work_struct rport_work;
  111. u32 port_id;
  112. };
  113. /**
  114. * enum fc_rport_event - Remote port events
  115. * @RPORT_EV_NONE: No event
  116. * @RPORT_EV_READY: Remote port is ready for use
  117. * @RPORT_EV_FAILED: State machine failed, remote port is not ready
  118. * @RPORT_EV_STOP: Remote port has been stopped
  119. * @RPORT_EV_LOGO: Remote port logout (LOGO) sent
  120. */
  121. enum fc_rport_event {
  122. RPORT_EV_NONE = 0,
  123. RPORT_EV_READY,
  124. RPORT_EV_FAILED,
  125. RPORT_EV_STOP,
  126. RPORT_EV_LOGO
  127. };
  128. struct fc_rport_priv;
  129. /**
  130. * struct fc_rport_operations - Operations for a remote port
  131. * @event_callback: Function to be called for remote port events
  132. */
  133. struct fc_rport_operations {
  134. void (*event_callback)(struct fc_lport *, struct fc_rport_priv *,
  135. enum fc_rport_event);
  136. };
  137. /**
  138. * struct fc_rport_libfc_priv - libfc internal information about a remote port
  139. * @local_port: The associated local port
  140. * @rp_state: Indicates READY for I/O or DELETE when blocked
  141. * @flags: REC and RETRY supported flags
  142. * @e_d_tov: Error detect timeout value (in msec)
  143. * @r_a_tov: Resource allocation timeout value (in msec)
  144. */
  145. struct fc_rport_libfc_priv {
  146. struct fc_lport *local_port;
  147. enum fc_rport_state rp_state;
  148. u16 flags;
  149. #define FC_RP_FLAGS_REC_SUPPORTED (1 << 0)
  150. #define FC_RP_FLAGS_RETRY (1 << 1)
  151. unsigned int e_d_tov;
  152. unsigned int r_a_tov;
  153. };
  154. /**
  155. * struct fc_rport_priv - libfc remote port and discovery info
  156. * @local_port: The associated local port
  157. * @rport: The FC transport remote port
  158. * @kref: Reference counter
  159. * @rp_state: Enumeration that tracks progress of PLOGI, PRLI,
  160. * and RTV exchanges
  161. * @ids: The remote port identifiers and roles
  162. * @flags: REC and RETRY supported flags
  163. * @max_seq: Maximum number of concurrent sequences
  164. * @disc_id: The discovery identifier
  165. * @maxframe_size: The maximum frame size
  166. * @retries: The retry count for the current state
  167. * @e_d_tov: Error detect timeout value (in msec)
  168. * @r_a_tov: Resource allocation timeout value (in msec)
  169. * @rp_mutex: The mutex that protects the remote port
  170. * @retry_work: Handle for retries
  171. * @event_callback: Callback when READY, FAILED or LOGO states complete
  172. */
  173. struct fc_rport_priv {
  174. struct fc_lport *local_port;
  175. struct fc_rport *rport;
  176. struct kref kref;
  177. enum fc_rport_state rp_state;
  178. struct fc_rport_identifiers ids;
  179. u16 flags;
  180. u16 max_seq;
  181. u16 disc_id;
  182. u16 maxframe_size;
  183. unsigned int retries;
  184. unsigned int e_d_tov;
  185. unsigned int r_a_tov;
  186. struct mutex rp_mutex;
  187. struct delayed_work retry_work;
  188. enum fc_rport_event event;
  189. struct fc_rport_operations *ops;
  190. struct list_head peers;
  191. struct work_struct event_work;
  192. u32 supported_classes;
  193. };
  194. /**
  195. * struct fcoe_dev_stats - fcoe stats structure
  196. * @SecondsSinceLastReset: Seconds since the last reset
  197. * @TxFrames: Number of transmitted frames
  198. * @TxWords: Number of transmitted words
  199. * @RxFrames: Number of received frames
  200. * @RxWords: Number of received words
  201. * @ErrorFrames: Number of received error frames
  202. * @DumpedFrames: Number of dumped frames
  203. * @LinkFailureCount: Number of link failures
  204. * @LossOfSignalCount: Number for signal losses
  205. * @InvalidTxWordCount: Number of invalid transmitted words
  206. * @InvalidCRCCount: Number of invalid CRCs
  207. * @InputRequests: Number of input requests
  208. * @OutputRequests: Number of output requests
  209. * @ControlRequests: Number of control requests
  210. * @InputMegabytes: Number of received megabytes
  211. * @OutputMegabytes: Number of transmitted megabytes
  212. */
  213. struct fcoe_dev_stats {
  214. u64 SecondsSinceLastReset;
  215. u64 TxFrames;
  216. u64 TxWords;
  217. u64 RxFrames;
  218. u64 RxWords;
  219. u64 ErrorFrames;
  220. u64 DumpedFrames;
  221. u64 LinkFailureCount;
  222. u64 LossOfSignalCount;
  223. u64 InvalidTxWordCount;
  224. u64 InvalidCRCCount;
  225. u64 InputRequests;
  226. u64 OutputRequests;
  227. u64 ControlRequests;
  228. u64 InputMegabytes;
  229. u64 OutputMegabytes;
  230. };
  231. /**
  232. * struct fc_seq_els_data - ELS data used for passing ELS specific responses
  233. * @fp: The ELS frame
  234. * @reason: The reason for rejection
  235. * @explan: The explaination of the rejection
  236. *
  237. * Mainly used by the exchange manager layer.
  238. */
  239. struct fc_seq_els_data {
  240. struct fc_frame *fp;
  241. enum fc_els_rjt_reason reason;
  242. enum fc_els_rjt_explan explan;
  243. };
  244. /**
  245. * struct fc_fcp_pkt - FCP request structure (one for each scsi_cmnd request)
  246. * @lp: The associated local port
  247. * @state: The state of the I/O
  248. * @tgt_flags: Target's flags
  249. * @ref_cnt: Reference count
  250. * @scsi_pkt_lock: Lock to protect the SCSI packet (must be taken before the
  251. * host_lock if both are to be held at the same time)
  252. * @cmd: The SCSI command (set and clear with the host_lock held)
  253. * @list: Tracks queued commands (accessed with the host_lock held)
  254. * @timer: The command timer
  255. * @tm_done: Completion indicator
  256. * @wait_for_comp: Indicator to wait for completion of the I/O (in jiffies)
  257. * @start_time: Timestamp indicating the start of the I/O (in jiffies)
  258. * @end_time: Timestamp indicating the end of the I/O (in jiffies)
  259. * @last_pkt_time: Timestamp of the last frame received (in jiffies)
  260. * @data_len: The length of the data
  261. * @cdb_cmd: The CDB command
  262. * @xfer_len: The transfer length
  263. * @xfer_ddp: Indicates if this transfer used DDP (XID of the exchange
  264. * will be set here if DDP was setup)
  265. * @xfer_contig_end: The offset into the buffer if the buffer is contiguous
  266. * (Tx and Rx)
  267. * @max_payload: The maximum payload size (in bytes)
  268. * @io_status: SCSI result (upper 24 bits)
  269. * @cdb_status: CDB status
  270. * @status_code: FCP I/O status
  271. * @scsi_comp_flags: Completion flags (bit 3 Underrun bit 2: overrun)
  272. * @req_flags: Request flags (bit 0: read bit:1 write)
  273. * @scsi_resid: SCSI residule length
  274. * @rport: The remote port that the SCSI command is targeted at
  275. * @seq_ptr: The sequence that will carry the SCSI command
  276. * @recov_retry: Number of recovery retries
  277. * @recov_seq: The sequence for REC or SRR
  278. */
  279. struct fc_fcp_pkt {
  280. /* Housekeeping information */
  281. struct fc_lport *lp;
  282. u16 state;
  283. u16 tgt_flags;
  284. atomic_t ref_cnt;
  285. spinlock_t scsi_pkt_lock;
  286. /* SCSI I/O related information */
  287. struct scsi_cmnd *cmd;
  288. struct list_head list;
  289. /* Timeout related information */
  290. struct timer_list timer;
  291. struct completion tm_done;
  292. int wait_for_comp;
  293. unsigned long start_time;
  294. unsigned long end_time;
  295. unsigned long last_pkt_time;
  296. /* SCSI command and data transfer information */
  297. u32 data_len;
  298. /* Transport related veriables */
  299. struct fcp_cmnd cdb_cmd;
  300. size_t xfer_len;
  301. u16 xfer_ddp;
  302. u32 xfer_contig_end;
  303. u16 max_payload;
  304. /* SCSI/FCP return status */
  305. u32 io_status;
  306. u8 cdb_status;
  307. u8 status_code;
  308. u8 scsi_comp_flags;
  309. u32 req_flags;
  310. u32 scsi_resid;
  311. /* Associated structures */
  312. struct fc_rport *rport;
  313. struct fc_seq *seq_ptr;
  314. /* Error Processing information */
  315. u8 recov_retry;
  316. struct fc_seq *recov_seq;
  317. };
  318. /*
  319. * Structure and function definitions for managing Fibre Channel Exchanges
  320. * and Sequences
  321. *
  322. * fc_exch holds state for one exchange and links to its active sequence.
  323. *
  324. * fc_seq holds the state for an individual sequence.
  325. */
  326. struct fc_exch_mgr;
  327. struct fc_exch_mgr_anchor;
  328. extern u16 fc_cpu_mask; /* cpu mask for possible cpus */
  329. /**
  330. * struct fc_seq - FC sequence
  331. * @id: The sequence ID
  332. * @ssb_stat: Status flags for the sequence status block (SSB)
  333. * @cnt: Number of frames sent so far
  334. * @rec_data: FC-4 value for REC
  335. */
  336. struct fc_seq {
  337. u8 id;
  338. u16 ssb_stat;
  339. u16 cnt;
  340. u32 rec_data;
  341. };
  342. #define FC_EX_DONE (1 << 0) /* ep is completed */
  343. #define FC_EX_RST_CLEANUP (1 << 1) /* reset is forcing completion */
  344. /**
  345. * struct fc_exch - Fibre Channel Exchange
  346. * @em: Exchange manager
  347. * @pool: Exchange pool
  348. * @state: The exchange's state
  349. * @xid: The exchange ID
  350. * @ex_list: Handle used by the EM to track free exchanges
  351. * @ex_lock: Lock that protects the exchange
  352. * @ex_refcnt: Reference count
  353. * @timeout_work: Handle for timeout handler
  354. * @lp: The local port that this exchange is on
  355. * @oxid: Originator's exchange ID
  356. * @rxid: Responder's exchange ID
  357. * @oid: Originator's FCID
  358. * @sid: Source FCID
  359. * @did: Destination FCID
  360. * @esb_stat: ESB exchange status
  361. * @r_a_tov: Resouce allocation time out value (in msecs)
  362. * @seq_id: The next sequence ID to use
  363. * @f_ctl: F_CTL flags for the sequence
  364. * @fh_type: The frame type
  365. * @class: The class of service
  366. * @seq: The sequence in use on this exchange
  367. * @resp: Callback for responses on this exchange
  368. * @destructor: Called when destroying the exchange
  369. * @arg: Passed as a void pointer to the resp() callback
  370. *
  371. * Locking notes: The ex_lock protects following items:
  372. * state, esb_stat, f_ctl, seq.ssb_stat
  373. * seq_id
  374. * sequence allocation
  375. */
  376. struct fc_exch {
  377. struct fc_exch_mgr *em;
  378. struct fc_exch_pool *pool;
  379. u32 state;
  380. u16 xid;
  381. struct list_head ex_list;
  382. spinlock_t ex_lock;
  383. atomic_t ex_refcnt;
  384. struct delayed_work timeout_work;
  385. struct fc_lport *lp;
  386. u16 oxid;
  387. u16 rxid;
  388. u32 oid;
  389. u32 sid;
  390. u32 did;
  391. u32 esb_stat;
  392. u32 r_a_tov;
  393. u8 seq_id;
  394. u32 f_ctl;
  395. u8 fh_type;
  396. enum fc_class class;
  397. struct fc_seq seq;
  398. void (*resp)(struct fc_seq *, struct fc_frame *, void *);
  399. void *arg;
  400. void (*destructor)(struct fc_seq *, void *);
  401. };
  402. #define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
  403. struct libfc_function_template {
  404. /*
  405. * Interface to send a FC frame
  406. *
  407. * STATUS: REQUIRED
  408. */
  409. int (*frame_send)(struct fc_lport *, struct fc_frame *);
  410. /*
  411. * Interface to send ELS/CT frames
  412. *
  413. * STATUS: OPTIONAL
  414. */
  415. struct fc_seq *(*elsct_send)(struct fc_lport *, u32 did,
  416. struct fc_frame *, unsigned int op,
  417. void (*resp)(struct fc_seq *,
  418. struct fc_frame *, void *arg),
  419. void *arg, u32 timer_msec);
  420. /*
  421. * Send the FC frame payload using a new exchange and sequence.
  422. *
  423. * The exchange response handler is set in this routine to resp()
  424. * function pointer. It can be called in two scenarios: if a timeout
  425. * occurs or if a response frame is received for the exchange. The
  426. * fc_frame pointer in response handler will also indicate timeout
  427. * as error using IS_ERR related macros.
  428. *
  429. * The exchange destructor handler is also set in this routine.
  430. * The destructor handler is invoked by EM layer when exchange
  431. * is about to free, this can be used by caller to free its
  432. * resources along with exchange free.
  433. *
  434. * The arg is passed back to resp and destructor handler.
  435. *
  436. * The timeout value (in msec) for an exchange is set if non zero
  437. * timer_msec argument is specified. The timer is canceled when
  438. * it fires or when the exchange is done. The exchange timeout handler
  439. * is registered by EM layer.
  440. *
  441. * STATUS: OPTIONAL
  442. */
  443. struct fc_seq *(*exch_seq_send)(struct fc_lport *, struct fc_frame *,
  444. void (*resp)(struct fc_seq *,
  445. struct fc_frame *,
  446. void *),
  447. void (*destructor)(struct fc_seq *,
  448. void *),
  449. void *, unsigned int timer_msec);
  450. /*
  451. * Sets up the DDP context for a given exchange id on the given
  452. * scatterlist if LLD supports DDP for large receive.
  453. *
  454. * STATUS: OPTIONAL
  455. */
  456. int (*ddp_setup)(struct fc_lport *, u16, struct scatterlist *,
  457. unsigned int);
  458. /*
  459. * Completes the DDP transfer and returns the length of data DDPed
  460. * for the given exchange id.
  461. *
  462. * STATUS: OPTIONAL
  463. */
  464. int (*ddp_done)(struct fc_lport *, u16);
  465. /*
  466. * Send a frame using an existing sequence and exchange.
  467. *
  468. * STATUS: OPTIONAL
  469. */
  470. int (*seq_send)(struct fc_lport *, struct fc_seq *,
  471. struct fc_frame *);
  472. /*
  473. * Send an ELS response using infomation from a previous
  474. * exchange and sequence.
  475. *
  476. * STATUS: OPTIONAL
  477. */
  478. void (*seq_els_rsp_send)(struct fc_seq *, enum fc_els_cmd,
  479. struct fc_seq_els_data *);
  480. /*
  481. * Abort an exchange and sequence. Generally called because of a
  482. * exchange timeout or an abort from the upper layer.
  483. *
  484. * A timer_msec can be specified for abort timeout, if non-zero
  485. * timer_msec value is specified then exchange resp handler
  486. * will be called with timeout error if no response to abort.
  487. *
  488. * STATUS: OPTIONAL
  489. */
  490. int (*seq_exch_abort)(const struct fc_seq *,
  491. unsigned int timer_msec);
  492. /*
  493. * Indicate that an exchange/sequence tuple is complete and the memory
  494. * allocated for the related objects may be freed.
  495. *
  496. * STATUS: OPTIONAL
  497. */
  498. void (*exch_done)(struct fc_seq *);
  499. /*
  500. * Start a new sequence on the same exchange/sequence tuple.
  501. *
  502. * STATUS: OPTIONAL
  503. */
  504. struct fc_seq *(*seq_start_next)(struct fc_seq *);
  505. /*
  506. * Reset an exchange manager, completing all sequences and exchanges.
  507. * If s_id is non-zero, reset only exchanges originating from that FID.
  508. * If d_id is non-zero, reset only exchanges sending to that FID.
  509. *
  510. * STATUS: OPTIONAL
  511. */
  512. void (*exch_mgr_reset)(struct fc_lport *, u32 s_id, u32 d_id);
  513. /*
  514. * Flush the rport work queue. Generally used before shutdown.
  515. *
  516. * STATUS: OPTIONAL
  517. */
  518. void (*rport_flush_queue)(void);
  519. /*
  520. * Receive a frame for a local port.
  521. *
  522. * STATUS: OPTIONAL
  523. */
  524. void (*lport_recv)(struct fc_lport *, struct fc_seq *,
  525. struct fc_frame *);
  526. /*
  527. * Reset the local port.
  528. *
  529. * STATUS: OPTIONAL
  530. */
  531. int (*lport_reset)(struct fc_lport *);
  532. /*
  533. * Create a remote port with a given port ID
  534. *
  535. * STATUS: OPTIONAL
  536. */
  537. struct fc_rport_priv *(*rport_create)(struct fc_lport *, u32);
  538. /*
  539. * Initiates the RP state machine. It is called from the LP module.
  540. * This function will issue the following commands to the N_Port
  541. * identified by the FC ID provided.
  542. *
  543. * - PLOGI
  544. * - PRLI
  545. * - RTV
  546. *
  547. * STATUS: OPTIONAL
  548. */
  549. int (*rport_login)(struct fc_rport_priv *);
  550. /*
  551. * Logoff, and remove the rport from the transport if
  552. * it had been added. This will send a LOGO to the target.
  553. *
  554. * STATUS: OPTIONAL
  555. */
  556. int (*rport_logoff)(struct fc_rport_priv *);
  557. /*
  558. * Recieve a request from a remote port.
  559. *
  560. * STATUS: OPTIONAL
  561. */
  562. void (*rport_recv_req)(struct fc_seq *, struct fc_frame *,
  563. struct fc_lport *);
  564. /*
  565. * lookup an rport by it's port ID.
  566. *
  567. * STATUS: OPTIONAL
  568. */
  569. struct fc_rport_priv *(*rport_lookup)(const struct fc_lport *, u32);
  570. /*
  571. * Destroy an rport after final kref_put().
  572. * The argument is a pointer to the kref inside the fc_rport_priv.
  573. */
  574. void (*rport_destroy)(struct kref *);
  575. /*
  576. * Send a fcp cmd from fsp pkt.
  577. * Called with the SCSI host lock unlocked and irqs disabled.
  578. *
  579. * The resp handler is called when FCP_RSP received.
  580. *
  581. * STATUS: OPTIONAL
  582. */
  583. int (*fcp_cmd_send)(struct fc_lport *, struct fc_fcp_pkt *,
  584. void (*resp)(struct fc_seq *, struct fc_frame *,
  585. void *));
  586. /*
  587. * Cleanup the FCP layer, used durring link down and reset
  588. *
  589. * STATUS: OPTIONAL
  590. */
  591. void (*fcp_cleanup)(struct fc_lport *);
  592. /*
  593. * Abort all I/O on a local port
  594. *
  595. * STATUS: OPTIONAL
  596. */
  597. void (*fcp_abort_io)(struct fc_lport *);
  598. /*
  599. * Receive a request for the discovery layer.
  600. *
  601. * STATUS: OPTIONAL
  602. */
  603. void (*disc_recv_req)(struct fc_seq *, struct fc_frame *,
  604. struct fc_lport *);
  605. /*
  606. * Start discovery for a local port.
  607. *
  608. * STATUS: OPTIONAL
  609. */
  610. void (*disc_start)(void (*disc_callback)(struct fc_lport *,
  611. enum fc_disc_event),
  612. struct fc_lport *);
  613. /*
  614. * Stop discovery for a given lport. This will remove
  615. * all discovered rports
  616. *
  617. * STATUS: OPTIONAL
  618. */
  619. void (*disc_stop) (struct fc_lport *);
  620. /*
  621. * Stop discovery for a given lport. This will block
  622. * until all discovered rports are deleted from the
  623. * FC transport class
  624. *
  625. * STATUS: OPTIONAL
  626. */
  627. void (*disc_stop_final) (struct fc_lport *);
  628. };
  629. /**
  630. * struct fc_disc - Discovery context
  631. * @retry_count: Number of retries
  632. * @pending: 1 if discovery is pending, 0 if not
  633. * @requesting: 1 if discovery has been requested, 0 if not
  634. * @seq_count: Number of sequences used for discovery
  635. * @buf_len: Length of the discovery buffer
  636. * @disc_id: Discovery ID
  637. * @rports: List of discovered remote ports
  638. * @lport: The local port that discovery is for
  639. * @disc_mutex: Mutex that protects the discovery context
  640. * @partial_buf: Partial name buffer (if names are returned
  641. * in multiple frames)
  642. * @disc_work: handle for delayed work context
  643. * @disc_callback: Callback routine called when discovery completes
  644. */
  645. struct fc_disc {
  646. unsigned char retry_count;
  647. unsigned char pending;
  648. unsigned char requested;
  649. unsigned short seq_count;
  650. unsigned char buf_len;
  651. u16 disc_id;
  652. struct list_head rports;
  653. struct fc_lport *lport;
  654. struct mutex disc_mutex;
  655. struct fc_gpn_ft_resp partial_buf;
  656. struct delayed_work disc_work;
  657. void (*disc_callback)(struct fc_lport *,
  658. enum fc_disc_event);
  659. };
  660. /**
  661. * struct fc_lport - Local port
  662. * @host: The SCSI host associated with a local port
  663. * @ema_list: Exchange manager anchor list
  664. * @dns_rdata: The directory server remote port
  665. * @ptp_rdata: Point to point remote port
  666. * @scsi_priv: FCP layer internal data
  667. * @disc: Discovery context
  668. * @vports: Child vports if N_Port
  669. * @vport: Parent vport if VN_Port
  670. * @tt: Libfc function template
  671. * @link_up: Link state (1 = link up, 0 = link down)
  672. * @qfull: Queue state (1 queue is full, 0 queue is not full)
  673. * @state: Identifies the state
  674. * @boot_time: Timestamp indicating when the local port came online
  675. * @host_stats: SCSI host statistics
  676. * @dev_stats: FCoE device stats (TODO: libfc should not be
  677. * FCoE aware)
  678. * @retry_count: Number of retries in the current state
  679. * @wwpn: World Wide Port Name
  680. * @wwnn: World Wide Node Name
  681. * @service_params: Common service parameters
  682. * @e_d_tov: Error detection timeout value
  683. * @r_a_tov: Resouce allocation timeout value
  684. * @rnid_gen: RNID information
  685. * @sg_supp: Indicates if scatter gather is supported
  686. * @seq_offload: Indicates if sequence offload is supported
  687. * @crc_offload: Indicates if CRC offload is supported
  688. * @lro_enabled: Indicates if large receive offload is supported
  689. * @does_npiv: Supports multiple vports
  690. * @npiv_enabled: Switch/fabric allows NPIV
  691. * @mfs: The maximum Fibre Channel payload size
  692. * @max_retry_count: The maximum retry attempts
  693. * @max_rport_retry_count: The maximum remote port retry attempts
  694. * @lro_xid: The maximum XID for LRO
  695. * @lso_max: The maximum large offload send size
  696. * @fcts: FC-4 type mask
  697. * @lp_mutex: Mutex to protect the local port
  698. * @list: Handle for list of local ports
  699. * @retry_work: Handle to local port for delayed retry context
  700. */
  701. struct fc_lport {
  702. /* Associations */
  703. struct Scsi_Host *host;
  704. struct list_head ema_list;
  705. struct fc_rport_priv *dns_rdata;
  706. struct fc_rport_priv *ptp_rdata;
  707. void *scsi_priv;
  708. struct fc_disc disc;
  709. /* Virtual port information */
  710. struct list_head vports;
  711. struct fc_vport *vport;
  712. /* Operational Information */
  713. struct libfc_function_template tt;
  714. u8 link_up;
  715. u8 qfull;
  716. enum fc_lport_state state;
  717. unsigned long boot_time;
  718. struct fc_host_statistics host_stats;
  719. struct fcoe_dev_stats *dev_stats;
  720. u8 retry_count;
  721. /* Fabric information */
  722. u64 wwpn;
  723. u64 wwnn;
  724. unsigned int service_params;
  725. unsigned int e_d_tov;
  726. unsigned int r_a_tov;
  727. struct fc_els_rnid_gen rnid_gen;
  728. /* Capabilities */
  729. u32 sg_supp:1;
  730. u32 seq_offload:1;
  731. u32 crc_offload:1;
  732. u32 lro_enabled:1;
  733. u32 does_npiv:1;
  734. u32 npiv_enabled:1;
  735. u32 mfs;
  736. u8 max_retry_count;
  737. u8 max_rport_retry_count;
  738. u16 link_speed;
  739. u16 link_supported_speeds;
  740. u16 lro_xid;
  741. unsigned int lso_max;
  742. struct fc_ns_fts fcts;
  743. /* Miscellaneous */
  744. struct mutex lp_mutex;
  745. struct list_head list;
  746. struct delayed_work retry_work;
  747. };
  748. /*
  749. * FC_LPORT HELPER FUNCTIONS
  750. *****************************/
  751. /**
  752. * fc_lport_test_ready() - Determine if a local port is in the READY state
  753. * @lport: The local port to test
  754. */
  755. static inline int fc_lport_test_ready(struct fc_lport *lport)
  756. {
  757. return lport->state == LPORT_ST_READY;
  758. }
  759. /**
  760. * fc_set_wwnn() - Set the World Wide Node Name of a local port
  761. * @lport: The local port whose WWNN is to be set
  762. * @wwnn: The new WWNN
  763. */
  764. static inline void fc_set_wwnn(struct fc_lport *lport, u64 wwnn)
  765. {
  766. lport->wwnn = wwnn;
  767. }
  768. /**
  769. * fc_set_wwpn() - Set the World Wide Port Name of a local port
  770. * @lport: The local port whose WWPN is to be set
  771. * @wwnn: The new WWPN
  772. */
  773. static inline void fc_set_wwpn(struct fc_lport *lport, u64 wwnn)
  774. {
  775. lport->wwpn = wwnn;
  776. }
  777. /**
  778. * fc_lport_state_enter() - Change a local port's state
  779. * @lport: The local port whose state is to change
  780. * @state: The new state
  781. */
  782. static inline void fc_lport_state_enter(struct fc_lport *lport,
  783. enum fc_lport_state state)
  784. {
  785. if (state != lport->state)
  786. lport->retry_count = 0;
  787. lport->state = state;
  788. }
  789. /**
  790. * fc_lport_init_stats() - Allocate per-CPU statistics for a local port
  791. * @lport: The local port whose statistics are to be initialized
  792. */
  793. static inline int fc_lport_init_stats(struct fc_lport *lport)
  794. {
  795. lport->dev_stats = alloc_percpu(struct fcoe_dev_stats);
  796. if (!lport->dev_stats)
  797. return -ENOMEM;
  798. return 0;
  799. }
  800. /**
  801. * fc_lport_free_stats() - Free memory for a local port's statistics
  802. * @lport: The local port whose statistics are to be freed
  803. */
  804. static inline void fc_lport_free_stats(struct fc_lport *lport)
  805. {
  806. free_percpu(lport->dev_stats);
  807. }
  808. /**
  809. * fc_lport_get_stats() - Get a local port's statistics
  810. * @lport: The local port whose statistics are to be retreived
  811. */
  812. static inline struct fcoe_dev_stats *fc_lport_get_stats(struct fc_lport *lport)
  813. {
  814. return per_cpu_ptr(lport->dev_stats, smp_processor_id());
  815. }
  816. /**
  817. * lport_priv() - Return the private data from a local port
  818. * @lport: The local port whose private data is to be retreived
  819. */
  820. static inline void *lport_priv(const struct fc_lport *lport)
  821. {
  822. return (void *)(lport + 1);
  823. }
  824. /**
  825. * libfc_host_alloc() - Allocate a Scsi_Host with room for a local port and
  826. * LLD private data
  827. * @sht: The SCSI host template
  828. * @priv_size: Size of private data
  829. *
  830. * Returns: libfc lport
  831. */
  832. static inline struct fc_lport *
  833. libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
  834. {
  835. struct fc_lport *lport;
  836. struct Scsi_Host *shost;
  837. shost = scsi_host_alloc(sht, sizeof(*lport) + priv_size);
  838. if (!shost)
  839. return NULL;
  840. lport = shost_priv(shost);
  841. lport->host = shost;
  842. INIT_LIST_HEAD(&lport->ema_list);
  843. INIT_LIST_HEAD(&lport->vports);
  844. return lport;
  845. }
  846. /*
  847. * FC_FCP HELPER FUNCTIONS
  848. *****************************/
  849. static inline bool fc_fcp_is_read(const struct fc_fcp_pkt *fsp)
  850. {
  851. if (fsp && fsp->cmd)
  852. return fsp->cmd->sc_data_direction == DMA_FROM_DEVICE;
  853. return false;
  854. }
  855. /*
  856. * LOCAL PORT LAYER
  857. *****************************/
  858. int fc_lport_init(struct fc_lport *);
  859. int fc_lport_destroy(struct fc_lport *);
  860. int fc_fabric_logoff(struct fc_lport *);
  861. int fc_fabric_login(struct fc_lport *);
  862. void __fc_linkup(struct fc_lport *);
  863. void fc_linkup(struct fc_lport *);
  864. void __fc_linkdown(struct fc_lport *);
  865. void fc_linkdown(struct fc_lport *);
  866. void fc_vport_setlink(struct fc_lport *);
  867. void fc_vports_linkchange(struct fc_lport *);
  868. int fc_lport_config(struct fc_lport *);
  869. int fc_lport_reset(struct fc_lport *);
  870. int fc_set_mfs(struct fc_lport *, u32 mfs);
  871. struct fc_lport *libfc_vport_create(struct fc_vport *, int privsize);
  872. struct fc_lport *fc_vport_id_lookup(struct fc_lport *, u32 port_id);
  873. int fc_lport_bsg_request(struct fc_bsg_job *);
  874. /*
  875. * REMOTE PORT LAYER
  876. *****************************/
  877. int fc_rport_init(struct fc_lport *);
  878. void fc_rport_terminate_io(struct fc_rport *);
  879. /*
  880. * DISCOVERY LAYER
  881. *****************************/
  882. int fc_disc_init(struct fc_lport *);
  883. /*
  884. * FCP LAYER
  885. *****************************/
  886. int fc_fcp_init(struct fc_lport *);
  887. void fc_fcp_destroy(struct fc_lport *);
  888. /*
  889. * SCSI INTERACTION LAYER
  890. *****************************/
  891. int fc_queuecommand(struct scsi_cmnd *,
  892. void (*done)(struct scsi_cmnd *));
  893. int fc_eh_abort(struct scsi_cmnd *);
  894. int fc_eh_device_reset(struct scsi_cmnd *);
  895. int fc_eh_host_reset(struct scsi_cmnd *);
  896. int fc_slave_alloc(struct scsi_device *);
  897. int fc_change_queue_depth(struct scsi_device *, int qdepth, int reason);
  898. int fc_change_queue_type(struct scsi_device *, int tag_type);
  899. /*
  900. * ELS/CT interface
  901. *****************************/
  902. int fc_elsct_init(struct fc_lport *);
  903. struct fc_seq *fc_elsct_send(struct fc_lport *, u32 did,
  904. struct fc_frame *,
  905. unsigned int op,
  906. void (*resp)(struct fc_seq *,
  907. struct fc_frame *,
  908. void *arg),
  909. void *arg, u32 timer_msec);
  910. void fc_lport_flogi_resp(struct fc_seq *, struct fc_frame *, void *);
  911. void fc_lport_logo_resp(struct fc_seq *, struct fc_frame *, void *);
  912. /*
  913. * EXCHANGE MANAGER LAYER
  914. *****************************/
  915. int fc_exch_init(struct fc_lport *);
  916. struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *,
  917. struct fc_exch_mgr *,
  918. bool (*match)(struct fc_frame *));
  919. void fc_exch_mgr_del(struct fc_exch_mgr_anchor *);
  920. int fc_exch_mgr_list_clone(struct fc_lport *src, struct fc_lport *dst);
  921. struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *, enum fc_class class,
  922. u16 min_xid, u16 max_xid,
  923. bool (*match)(struct fc_frame *));
  924. void fc_exch_mgr_free(struct fc_lport *);
  925. void fc_exch_recv(struct fc_lport *, struct fc_frame *);
  926. void fc_exch_mgr_reset(struct fc_lport *, u32 s_id, u32 d_id);
  927. /*
  928. * Functions for fc_functions_template
  929. */
  930. void fc_get_host_speed(struct Scsi_Host *);
  931. void fc_get_host_port_type(struct Scsi_Host *);
  932. void fc_get_host_port_state(struct Scsi_Host *);
  933. void fc_set_rport_loss_tmo(struct fc_rport *, u32 timeout);
  934. struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *);
  935. #endif /* _LIBFC_H_ */