libfc.h 31 KB

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