libfc.h 31 KB

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