libfc.h 31 KB

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