libfc.h 32 KB

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