libfc.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092
  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/fc/fc_fcp.h>
  27. #include <scsi/fc/fc_ns.h>
  28. #include <scsi/fc/fc_els.h>
  29. #include <scsi/fc/fc_gs.h>
  30. #include <scsi/fc_frame.h>
  31. #define FC_LIBFC_LOGGING 0x01 /* General logging, not categorized */
  32. #define FC_LPORT_LOGGING 0x02 /* lport layer logging */
  33. #define FC_DISC_LOGGING 0x04 /* discovery layer logging */
  34. #define FC_RPORT_LOGGING 0x08 /* rport layer logging */
  35. #define FC_FCP_LOGGING 0x10 /* I/O path logging */
  36. #define FC_EM_LOGGING 0x20 /* Exchange Manager logging */
  37. #define FC_EXCH_LOGGING 0x40 /* Exchange/Sequence logging */
  38. #define FC_SCSI_LOGGING 0x80 /* SCSI logging (mostly error handling) */
  39. extern unsigned int fc_debug_logging;
  40. #define FC_CHECK_LOGGING(LEVEL, CMD) \
  41. do { \
  42. if (unlikely(fc_debug_logging & LEVEL)) \
  43. do { \
  44. CMD; \
  45. } while (0); \
  46. } while (0)
  47. #define FC_LIBFC_DBG(fmt, args...) \
  48. FC_CHECK_LOGGING(FC_LIBFC_LOGGING, \
  49. printk(KERN_INFO "libfc: " fmt, ##args))
  50. #define FC_LPORT_DBG(lport, fmt, args...) \
  51. FC_CHECK_LOGGING(FC_LPORT_LOGGING, \
  52. printk(KERN_INFO "host%u: lport %6x: " fmt, \
  53. (lport)->host->host_no, \
  54. fc_host_port_id((lport)->host), ##args))
  55. #define FC_DISC_DBG(disc, fmt, args...) \
  56. FC_CHECK_LOGGING(FC_DISC_LOGGING, \
  57. printk(KERN_INFO "host%u: disc: " fmt, \
  58. (disc)->lport->host->host_no, \
  59. ##args))
  60. #define FC_RPORT_ID_DBG(lport, port_id, fmt, args...) \
  61. FC_CHECK_LOGGING(FC_RPORT_LOGGING, \
  62. printk(KERN_INFO "host%u: rport %6x: " fmt, \
  63. (lport)->host->host_no, \
  64. (port_id), ##args))
  65. #define FC_RPORT_DBG(rdata, fmt, args...) \
  66. FC_RPORT_ID_DBG((rdata)->local_port, (rdata)->ids.port_id, fmt, ##args)
  67. #define FC_FCP_DBG(pkt, fmt, args...) \
  68. FC_CHECK_LOGGING(FC_FCP_LOGGING, \
  69. printk(KERN_INFO "host%u: fcp: %6x: " fmt, \
  70. (pkt)->lp->host->host_no, \
  71. pkt->rport->port_id, ##args))
  72. #define FC_EXCH_DBG(exch, fmt, args...) \
  73. FC_CHECK_LOGGING(FC_EXCH_LOGGING, \
  74. printk(KERN_INFO "host%u: xid %4x: " fmt, \
  75. (exch)->lp->host->host_no, \
  76. exch->xid, ##args))
  77. #define FC_SCSI_DBG(lport, fmt, args...) \
  78. FC_CHECK_LOGGING(FC_SCSI_LOGGING, \
  79. printk(KERN_INFO "host%u: scsi: " fmt, \
  80. (lport)->host->host_no, ##args))
  81. /*
  82. * libfc error codes
  83. */
  84. #define FC_NO_ERR 0 /* no error */
  85. #define FC_EX_TIMEOUT 1 /* Exchange timeout */
  86. #define FC_EX_CLOSED 2 /* Exchange closed */
  87. /* some helpful macros */
  88. #define ntohll(x) be64_to_cpu(x)
  89. #define htonll(x) cpu_to_be64(x)
  90. #define ntoh24(p) (((p)[0] << 16) | ((p)[1] << 8) | ((p)[2]))
  91. #define hton24(p, v) do { \
  92. p[0] = (((v) >> 16) & 0xFF); \
  93. p[1] = (((v) >> 8) & 0xFF); \
  94. p[2] = ((v) & 0xFF); \
  95. } while (0)
  96. /*
  97. * FC HBA status
  98. */
  99. enum fc_lport_state {
  100. LPORT_ST_DISABLED = 0,
  101. LPORT_ST_FLOGI,
  102. LPORT_ST_DNS,
  103. LPORT_ST_RPN_ID,
  104. LPORT_ST_RFT_ID,
  105. LPORT_ST_SCR,
  106. LPORT_ST_READY,
  107. LPORT_ST_LOGO,
  108. LPORT_ST_RESET
  109. };
  110. enum fc_disc_event {
  111. DISC_EV_NONE = 0,
  112. DISC_EV_SUCCESS,
  113. DISC_EV_FAILED
  114. };
  115. enum fc_rport_state {
  116. RPORT_ST_INIT, /* initialized */
  117. RPORT_ST_PLOGI, /* waiting for PLOGI completion */
  118. RPORT_ST_PRLI, /* waiting for PRLI completion */
  119. RPORT_ST_RTV, /* waiting for RTV completion */
  120. RPORT_ST_READY, /* ready for use */
  121. RPORT_ST_LOGO, /* port logout sent */
  122. RPORT_ST_DELETE, /* port being deleted */
  123. };
  124. /**
  125. * struct fc_disc_port - temporary discovery port to hold rport identifiers
  126. * @lp: Fibre Channel host port instance
  127. * @peers: Node for list management during discovery and RSCN processing
  128. * @rport_work: Work struct for starting the rport state machine
  129. * @port_id: Port ID of the discovered port
  130. */
  131. struct fc_disc_port {
  132. struct fc_lport *lp;
  133. struct list_head peers;
  134. struct work_struct rport_work;
  135. u32 port_id;
  136. };
  137. enum fc_rport_event {
  138. RPORT_EV_NONE = 0,
  139. RPORT_EV_READY,
  140. RPORT_EV_FAILED,
  141. RPORT_EV_STOP,
  142. RPORT_EV_LOGO
  143. };
  144. struct fc_rport_priv;
  145. struct fc_rport_operations {
  146. void (*event_callback)(struct fc_lport *, struct fc_rport_priv *,
  147. enum fc_rport_event);
  148. };
  149. /**
  150. * struct fc_rport_libfc_priv - libfc internal information about a remote port
  151. * @local_port: Fibre Channel host port instance
  152. * @rp_state: indicates READY for I/O or DELETE when blocked.
  153. * @flags: REC and RETRY supported flags
  154. * @e_d_tov: error detect timeout value (in msec)
  155. * @r_a_tov: resource allocation timeout value (in msec)
  156. */
  157. struct fc_rport_libfc_priv {
  158. struct fc_lport *local_port;
  159. enum fc_rport_state rp_state;
  160. u16 flags;
  161. #define FC_RP_FLAGS_REC_SUPPORTED (1 << 0)
  162. #define FC_RP_FLAGS_RETRY (1 << 1)
  163. unsigned int e_d_tov;
  164. unsigned int r_a_tov;
  165. };
  166. /**
  167. * struct fc_rport_priv - libfc rport and discovery info about a remote port
  168. * @local_port: Fibre Channel host port instance
  169. * @rport: transport remote port
  170. * @kref: reference counter
  171. * @rp_state: state tracks progress of PLOGI, PRLI, and RTV exchanges
  172. * @ids: remote port identifiers and roles
  173. * @flags: REC and RETRY supported flags
  174. * @max_seq: maximum number of concurrent sequences
  175. * @disc_id: discovery identifier
  176. * @maxframe_size: maximum frame size
  177. * @retries: retry count in current state
  178. * @e_d_tov: error detect timeout value (in msec)
  179. * @r_a_tov: resource allocation timeout value (in msec)
  180. * @rp_mutex: mutex protects rport
  181. * @retry_work:
  182. * @event_callback: Callback for rport READY, FAILED or LOGO
  183. */
  184. struct fc_rport_priv {
  185. struct fc_lport *local_port;
  186. struct fc_rport *rport;
  187. struct kref kref;
  188. enum fc_rport_state rp_state;
  189. struct fc_rport_identifiers ids;
  190. u16 flags;
  191. u16 max_seq;
  192. u16 disc_id;
  193. u16 maxframe_size;
  194. unsigned int retries;
  195. unsigned int e_d_tov;
  196. unsigned int r_a_tov;
  197. struct mutex rp_mutex;
  198. struct delayed_work retry_work;
  199. enum fc_rport_event event;
  200. struct fc_rport_operations *ops;
  201. struct list_head peers;
  202. struct work_struct event_work;
  203. u32 supported_classes;
  204. };
  205. /*
  206. * fcoe stats structure
  207. */
  208. struct fcoe_dev_stats {
  209. u64 SecondsSinceLastReset;
  210. u64 TxFrames;
  211. u64 TxWords;
  212. u64 RxFrames;
  213. u64 RxWords;
  214. u64 ErrorFrames;
  215. u64 DumpedFrames;
  216. u64 LinkFailureCount;
  217. u64 LossOfSignalCount;
  218. u64 InvalidTxWordCount;
  219. u64 InvalidCRCCount;
  220. u64 InputRequests;
  221. u64 OutputRequests;
  222. u64 ControlRequests;
  223. u64 InputMegabytes;
  224. u64 OutputMegabytes;
  225. };
  226. /*
  227. * els data is used for passing ELS respone specific
  228. * data to send ELS response mainly using infomation
  229. * in exchange and sequence in EM layer.
  230. */
  231. struct fc_seq_els_data {
  232. struct fc_frame *fp;
  233. enum fc_els_rjt_reason reason;
  234. enum fc_els_rjt_explan explan;
  235. };
  236. /*
  237. * FCP request structure, one for each scsi cmd request
  238. */
  239. struct fc_fcp_pkt {
  240. /*
  241. * housekeeping stuff
  242. */
  243. struct fc_lport *lp; /* handle to hba struct */
  244. u16 state; /* scsi_pkt state state */
  245. u16 tgt_flags; /* target flags */
  246. atomic_t ref_cnt; /* fcp pkt ref count */
  247. spinlock_t scsi_pkt_lock; /* Must be taken before the host lock
  248. * if both are held at the same time */
  249. /*
  250. * SCSI I/O related stuff
  251. */
  252. struct scsi_cmnd *cmd; /* scsi command pointer. set/clear
  253. * under host lock */
  254. struct list_head list; /* tracks queued commands. access under
  255. * host lock */
  256. /*
  257. * timeout related stuff
  258. */
  259. struct timer_list timer; /* command timer */
  260. struct completion tm_done;
  261. int wait_for_comp;
  262. unsigned long start_time; /* start jiffie */
  263. unsigned long end_time; /* end jiffie */
  264. unsigned long last_pkt_time; /* jiffies of last frame received */
  265. /*
  266. * scsi cmd and data transfer information
  267. */
  268. u32 data_len;
  269. /*
  270. * transport related veriables
  271. */
  272. struct fcp_cmnd cdb_cmd;
  273. size_t xfer_len;
  274. u16 xfer_ddp; /* this xfer is ddped */
  275. u32 xfer_contig_end; /* offset of end of contiguous xfer */
  276. u16 max_payload; /* max payload size in bytes */
  277. /*
  278. * scsi/fcp return status
  279. */
  280. u32 io_status; /* SCSI result upper 24 bits */
  281. u8 cdb_status;
  282. u8 status_code; /* FCP I/O status */
  283. /* bit 3 Underrun bit 2: overrun */
  284. u8 scsi_comp_flags;
  285. u32 req_flags; /* bit 0: read bit:1 write */
  286. u32 scsi_resid; /* residule length */
  287. struct fc_rport *rport; /* remote port pointer */
  288. struct fc_seq *seq_ptr; /* current sequence pointer */
  289. /*
  290. * Error Processing
  291. */
  292. u8 recov_retry; /* count of recovery retries */
  293. struct fc_seq *recov_seq; /* sequence for REC or SRR */
  294. };
  295. /*
  296. * FC_FCP HELPER FUNCTIONS
  297. *****************************/
  298. static inline bool fc_fcp_is_read(const struct fc_fcp_pkt *fsp)
  299. {
  300. if (fsp && fsp->cmd)
  301. return fsp->cmd->sc_data_direction == DMA_FROM_DEVICE;
  302. return false;
  303. }
  304. /*
  305. * Structure and function definitions for managing Fibre Channel Exchanges
  306. * and Sequences
  307. *
  308. * fc_exch holds state for one exchange and links to its active sequence.
  309. *
  310. * fc_seq holds the state for an individual sequence.
  311. */
  312. struct fc_exch_mgr;
  313. struct fc_exch_mgr_anchor;
  314. extern u16 fc_cpu_mask; /* cpu mask for possible cpus */
  315. /*
  316. * Sequence.
  317. */
  318. struct fc_seq {
  319. u8 id; /* seq ID */
  320. u16 ssb_stat; /* status flags for sequence status block */
  321. u16 cnt; /* frames sent so far on sequence */
  322. u32 rec_data; /* FC-4 value for REC */
  323. };
  324. #define FC_EX_DONE (1 << 0) /* ep is completed */
  325. #define FC_EX_RST_CLEANUP (1 << 1) /* reset is forcing completion */
  326. /*
  327. * Exchange.
  328. *
  329. * Locking notes: The ex_lock protects following items:
  330. * state, esb_stat, f_ctl, seq.ssb_stat
  331. * seq_id
  332. * sequence allocation
  333. */
  334. struct fc_exch {
  335. struct fc_exch_mgr *em; /* exchange manager */
  336. struct fc_exch_pool *pool; /* per cpu exches pool */
  337. u32 state; /* internal driver state */
  338. u16 xid; /* our exchange ID */
  339. struct list_head ex_list; /* free or busy list linkage */
  340. spinlock_t ex_lock; /* lock covering exchange state */
  341. atomic_t ex_refcnt; /* reference counter */
  342. struct delayed_work timeout_work; /* timer for upper level protocols */
  343. struct fc_lport *lp; /* fc device instance */
  344. u16 oxid; /* originator's exchange ID */
  345. u16 rxid; /* responder's exchange ID */
  346. u32 oid; /* originator's FCID */
  347. u32 sid; /* source FCID */
  348. u32 did; /* destination FCID */
  349. u32 esb_stat; /* exchange status for ESB */
  350. u32 r_a_tov; /* r_a_tov from rport (msec) */
  351. u8 seq_id; /* next sequence ID to use */
  352. u32 f_ctl; /* F_CTL flags for sequences */
  353. u8 fh_type; /* frame type */
  354. enum fc_class class; /* class of service */
  355. struct fc_seq seq; /* single sequence */
  356. /*
  357. * Handler for responses to this current exchange.
  358. */
  359. void (*resp)(struct fc_seq *, struct fc_frame *, void *);
  360. void (*destructor)(struct fc_seq *, void *);
  361. /*
  362. * arg is passed as void pointer to exchange
  363. * resp and destructor handlers
  364. */
  365. void *arg;
  366. };
  367. #define fc_seq_exch(sp) container_of(sp, struct fc_exch, seq)
  368. struct libfc_function_template {
  369. /*
  370. * Interface to send a FC frame
  371. *
  372. * STATUS: REQUIRED
  373. */
  374. int (*frame_send)(struct fc_lport *lp, struct fc_frame *fp);
  375. /*
  376. * Interface to send ELS/CT frames
  377. *
  378. * STATUS: OPTIONAL
  379. */
  380. struct fc_seq *(*elsct_send)(struct fc_lport *lport,
  381. u32 did,
  382. struct fc_frame *fp,
  383. unsigned int op,
  384. void (*resp)(struct fc_seq *,
  385. struct fc_frame *fp,
  386. void *arg),
  387. void *arg, u32 timer_msec);
  388. /*
  389. * Send the FC frame payload using a new exchange and sequence.
  390. *
  391. * The frame pointer with some of the header's fields must be
  392. * filled before calling exch_seq_send(), those fields are,
  393. *
  394. * - routing control
  395. * - FC port did
  396. * - FC port sid
  397. * - FC header type
  398. * - frame control
  399. * - parameter or relative offset
  400. *
  401. * The exchange response handler is set in this routine to resp()
  402. * function pointer. It can be called in two scenarios: if a timeout
  403. * occurs or if a response frame is received for the exchange. The
  404. * fc_frame pointer in response handler will also indicate timeout
  405. * as error using IS_ERR related macros.
  406. *
  407. * The exchange destructor handler is also set in this routine.
  408. * The destructor handler is invoked by EM layer when exchange
  409. * is about to free, this can be used by caller to free its
  410. * resources along with exchange free.
  411. *
  412. * The arg is passed back to resp and destructor handler.
  413. *
  414. * The timeout value (in msec) for an exchange is set if non zero
  415. * timer_msec argument is specified. The timer is canceled when
  416. * it fires or when the exchange is done. The exchange timeout handler
  417. * is registered by EM layer.
  418. *
  419. * STATUS: OPTIONAL
  420. */
  421. struct fc_seq *(*exch_seq_send)(struct fc_lport *lp,
  422. struct fc_frame *fp,
  423. void (*resp)(struct fc_seq *sp,
  424. struct fc_frame *fp,
  425. void *arg),
  426. void (*destructor)(struct fc_seq *sp,
  427. void *arg),
  428. void *arg, unsigned int timer_msec);
  429. /*
  430. * Sets up the DDP context for a given exchange id on the given
  431. * scatterlist if LLD supports DDP for large receive.
  432. *
  433. * STATUS: OPTIONAL
  434. */
  435. int (*ddp_setup)(struct fc_lport *lp, u16 xid,
  436. struct scatterlist *sgl, unsigned int sgc);
  437. /*
  438. * Completes the DDP transfer and returns the length of data DDPed
  439. * for the given exchange id.
  440. *
  441. * STATUS: OPTIONAL
  442. */
  443. int (*ddp_done)(struct fc_lport *lp, u16 xid);
  444. /*
  445. * Send a frame using an existing sequence and exchange.
  446. *
  447. * STATUS: OPTIONAL
  448. */
  449. int (*seq_send)(struct fc_lport *lp, struct fc_seq *sp,
  450. struct fc_frame *fp);
  451. /*
  452. * Send an ELS response using infomation from a previous
  453. * exchange and sequence.
  454. *
  455. * STATUS: OPTIONAL
  456. */
  457. void (*seq_els_rsp_send)(struct fc_seq *sp, enum fc_els_cmd els_cmd,
  458. struct fc_seq_els_data *els_data);
  459. /*
  460. * Abort an exchange and sequence. Generally called because of a
  461. * exchange timeout or an abort from the upper layer.
  462. *
  463. * A timer_msec can be specified for abort timeout, if non-zero
  464. * timer_msec value is specified then exchange resp handler
  465. * will be called with timeout error if no response to abort.
  466. *
  467. * STATUS: OPTIONAL
  468. */
  469. int (*seq_exch_abort)(const struct fc_seq *req_sp,
  470. unsigned int timer_msec);
  471. /*
  472. * Indicate that an exchange/sequence tuple is complete and the memory
  473. * allocated for the related objects may be freed.
  474. *
  475. * STATUS: OPTIONAL
  476. */
  477. void (*exch_done)(struct fc_seq *sp);
  478. /*
  479. * Start a new sequence on the same exchange/sequence tuple.
  480. *
  481. * STATUS: OPTIONAL
  482. */
  483. struct fc_seq *(*seq_start_next)(struct fc_seq *sp);
  484. /*
  485. * Reset an exchange manager, completing all sequences and exchanges.
  486. * If s_id is non-zero, reset only exchanges originating from that FID.
  487. * If d_id is non-zero, reset only exchanges sending to that FID.
  488. *
  489. * STATUS: OPTIONAL
  490. */
  491. void (*exch_mgr_reset)(struct fc_lport *,
  492. u32 s_id, u32 d_id);
  493. /*
  494. * Flush the rport work queue. Generally used before shutdown.
  495. *
  496. * STATUS: OPTIONAL
  497. */
  498. void (*rport_flush_queue)(void);
  499. /*
  500. * Receive a frame for a local port.
  501. *
  502. * STATUS: OPTIONAL
  503. */
  504. void (*lport_recv)(struct fc_lport *lp, struct fc_seq *sp,
  505. struct fc_frame *fp);
  506. /*
  507. * Reset the local port.
  508. *
  509. * STATUS: OPTIONAL
  510. */
  511. int (*lport_reset)(struct fc_lport *);
  512. /*
  513. * Create a remote port with a given port ID
  514. *
  515. * STATUS: OPTIONAL
  516. */
  517. struct fc_rport_priv *(*rport_create)(struct fc_lport *, u32);
  518. /*
  519. * Initiates the RP state machine. It is called from the LP module.
  520. * This function will issue the following commands to the N_Port
  521. * identified by the FC ID provided.
  522. *
  523. * - PLOGI
  524. * - PRLI
  525. * - RTV
  526. *
  527. * STATUS: OPTIONAL
  528. */
  529. int (*rport_login)(struct fc_rport_priv *);
  530. /*
  531. * Logoff, and remove the rport from the transport if
  532. * it had been added. This will send a LOGO to the target.
  533. *
  534. * STATUS: OPTIONAL
  535. */
  536. int (*rport_logoff)(struct fc_rport_priv *);
  537. /*
  538. * Recieve a request from a remote port.
  539. *
  540. * STATUS: OPTIONAL
  541. */
  542. void (*rport_recv_req)(struct fc_seq *, struct fc_frame *,
  543. struct fc_lport *);
  544. /*
  545. * lookup an rport by it's port ID.
  546. *
  547. * STATUS: OPTIONAL
  548. */
  549. struct fc_rport_priv *(*rport_lookup)(const struct fc_lport *, u32);
  550. /*
  551. * Destroy an rport after final kref_put().
  552. * The argument is a pointer to the kref inside the fc_rport_priv.
  553. */
  554. void (*rport_destroy)(struct kref *);
  555. /*
  556. * Send a fcp cmd from fsp pkt.
  557. * Called with the SCSI host lock unlocked and irqs disabled.
  558. *
  559. * The resp handler is called when FCP_RSP received.
  560. *
  561. * STATUS: OPTIONAL
  562. */
  563. int (*fcp_cmd_send)(struct fc_lport *lp, struct fc_fcp_pkt *fsp,
  564. void (*resp)(struct fc_seq *, struct fc_frame *fp,
  565. void *arg));
  566. /*
  567. * Cleanup the FCP layer, used durring link down and reset
  568. *
  569. * STATUS: OPTIONAL
  570. */
  571. void (*fcp_cleanup)(struct fc_lport *lp);
  572. /*
  573. * Abort all I/O on a local port
  574. *
  575. * STATUS: OPTIONAL
  576. */
  577. void (*fcp_abort_io)(struct fc_lport *lp);
  578. /*
  579. * Receive a request for the discovery layer.
  580. *
  581. * STATUS: OPTIONAL
  582. */
  583. void (*disc_recv_req)(struct fc_seq *,
  584. struct fc_frame *, struct fc_lport *);
  585. /*
  586. * Start discovery for a local port.
  587. *
  588. * STATUS: OPTIONAL
  589. */
  590. void (*disc_start)(void (*disc_callback)(struct fc_lport *,
  591. enum fc_disc_event),
  592. struct fc_lport *);
  593. /*
  594. * Stop discovery for a given lport. This will remove
  595. * all discovered rports
  596. *
  597. * STATUS: OPTIONAL
  598. */
  599. void (*disc_stop) (struct fc_lport *);
  600. /*
  601. * Stop discovery for a given lport. This will block
  602. * until all discovered rports are deleted from the
  603. * FC transport class
  604. *
  605. * STATUS: OPTIONAL
  606. */
  607. void (*disc_stop_final) (struct fc_lport *);
  608. };
  609. /* information used by the discovery layer */
  610. struct fc_disc {
  611. unsigned char retry_count;
  612. unsigned char pending;
  613. unsigned char requested;
  614. unsigned short seq_count;
  615. unsigned char buf_len;
  616. u16 disc_id;
  617. void (*disc_callback)(struct fc_lport *,
  618. enum fc_disc_event);
  619. struct list_head rports;
  620. struct fc_lport *lport;
  621. struct mutex disc_mutex;
  622. struct fc_gpn_ft_resp partial_buf; /* partial name buffer */
  623. struct delayed_work disc_work;
  624. };
  625. struct fc_lport {
  626. struct list_head list;
  627. /* Associations */
  628. struct Scsi_Host *host;
  629. struct list_head ema_list;
  630. struct fc_rport_priv *dns_rp;
  631. struct fc_rport_priv *ptp_rp;
  632. void *scsi_priv;
  633. struct fc_disc disc;
  634. /* Operational Information */
  635. struct libfc_function_template tt;
  636. u8 link_up;
  637. u8 qfull;
  638. enum fc_lport_state state;
  639. unsigned long boot_time;
  640. struct fc_host_statistics host_stats;
  641. struct fcoe_dev_stats *dev_stats;
  642. u64 wwpn;
  643. u64 wwnn;
  644. u8 retry_count;
  645. /* Capabilities */
  646. u32 sg_supp:1; /* scatter gather supported */
  647. u32 seq_offload:1; /* seq offload supported */
  648. u32 crc_offload:1; /* crc offload supported */
  649. u32 lro_enabled:1; /* large receive offload */
  650. u32 mfs; /* max FC payload size */
  651. unsigned int service_params;
  652. unsigned int e_d_tov;
  653. unsigned int r_a_tov;
  654. u8 max_retry_count;
  655. u8 max_rport_retry_count;
  656. u16 link_speed;
  657. u16 link_supported_speeds;
  658. u16 lro_xid; /* max xid for fcoe lro */
  659. unsigned int lso_max; /* max large send size */
  660. struct fc_ns_fts fcts; /* FC-4 type masks */
  661. struct fc_els_rnid_gen rnid_gen; /* RNID information */
  662. /* Semaphores */
  663. struct mutex lp_mutex;
  664. /* Miscellaneous */
  665. struct delayed_work retry_work;
  666. struct delayed_work disc_work;
  667. };
  668. /*
  669. * FC_LPORT HELPER FUNCTIONS
  670. *****************************/
  671. static inline int fc_lport_test_ready(struct fc_lport *lp)
  672. {
  673. return lp->state == LPORT_ST_READY;
  674. }
  675. static inline void fc_set_wwnn(struct fc_lport *lp, u64 wwnn)
  676. {
  677. lp->wwnn = wwnn;
  678. }
  679. static inline void fc_set_wwpn(struct fc_lport *lp, u64 wwnn)
  680. {
  681. lp->wwpn = wwnn;
  682. }
  683. static inline void fc_lport_state_enter(struct fc_lport *lp,
  684. enum fc_lport_state state)
  685. {
  686. if (state != lp->state)
  687. lp->retry_count = 0;
  688. lp->state = state;
  689. }
  690. static inline int fc_lport_init_stats(struct fc_lport *lp)
  691. {
  692. /* allocate per cpu stats block */
  693. lp->dev_stats = alloc_percpu(struct fcoe_dev_stats);
  694. if (!lp->dev_stats)
  695. return -ENOMEM;
  696. return 0;
  697. }
  698. static inline void fc_lport_free_stats(struct fc_lport *lp)
  699. {
  700. free_percpu(lp->dev_stats);
  701. }
  702. static inline struct fcoe_dev_stats *fc_lport_get_stats(struct fc_lport *lp)
  703. {
  704. return per_cpu_ptr(lp->dev_stats, smp_processor_id());
  705. }
  706. static inline void *lport_priv(const struct fc_lport *lp)
  707. {
  708. return (void *)(lp + 1);
  709. }
  710. /**
  711. * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport
  712. * @sht: ptr to the scsi host templ
  713. * @priv_size: size of private data after fc_lport
  714. *
  715. * Returns: ptr to Scsi_Host
  716. */
  717. static inline struct Scsi_Host *
  718. libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
  719. {
  720. return scsi_host_alloc(sht, sizeof(struct fc_lport) + priv_size);
  721. }
  722. /*
  723. * LOCAL PORT LAYER
  724. *****************************/
  725. int fc_lport_init(struct fc_lport *lp);
  726. /*
  727. * Destroy the specified local port by finding and freeing all
  728. * fc_rports associated with it and then by freeing the fc_lport
  729. * itself.
  730. */
  731. int fc_lport_destroy(struct fc_lport *lp);
  732. /*
  733. * Logout the specified local port from the fabric
  734. */
  735. int fc_fabric_logoff(struct fc_lport *lp);
  736. /*
  737. * Initiate the LP state machine. This handler will use fc_host_attr
  738. * to store the FLOGI service parameters, so fc_host_attr must be
  739. * initialized before calling this handler.
  740. */
  741. int fc_fabric_login(struct fc_lport *lp);
  742. /*
  743. * The link is up for the given local port.
  744. */
  745. void fc_linkup(struct fc_lport *);
  746. /*
  747. * Link is down for the given local port.
  748. */
  749. void fc_linkdown(struct fc_lport *);
  750. /*
  751. * Configure the local port.
  752. */
  753. int fc_lport_config(struct fc_lport *);
  754. /*
  755. * Reset the local port.
  756. */
  757. int fc_lport_reset(struct fc_lport *);
  758. /*
  759. * Set the mfs or reset
  760. */
  761. int fc_set_mfs(struct fc_lport *lp, u32 mfs);
  762. /*
  763. * REMOTE PORT LAYER
  764. *****************************/
  765. int fc_rport_init(struct fc_lport *lp);
  766. void fc_rport_terminate_io(struct fc_rport *rp);
  767. /*
  768. * DISCOVERY LAYER
  769. *****************************/
  770. int fc_disc_init(struct fc_lport *lp);
  771. /*
  772. * SCSI LAYER
  773. *****************************/
  774. /*
  775. * Initialize the SCSI block of libfc
  776. */
  777. int fc_fcp_init(struct fc_lport *);
  778. /*
  779. * This section provides an API which allows direct interaction
  780. * with the SCSI-ml. Each of these functions satisfies a function
  781. * pointer defined in Scsi_Host and therefore is always called
  782. * directly from the SCSI-ml.
  783. */
  784. int fc_queuecommand(struct scsi_cmnd *sc_cmd,
  785. void (*done)(struct scsi_cmnd *));
  786. /*
  787. * complete processing of a fcp packet
  788. *
  789. * This function may sleep if a fsp timer is pending.
  790. * The host lock must not be held by caller.
  791. */
  792. void fc_fcp_complete(struct fc_fcp_pkt *fsp);
  793. /*
  794. * Send an ABTS frame to the target device. The sc_cmd argument
  795. * is a pointer to the SCSI command to be aborted.
  796. */
  797. int fc_eh_abort(struct scsi_cmnd *sc_cmd);
  798. /*
  799. * Reset a LUN by sending send the tm cmd to the target.
  800. */
  801. int fc_eh_device_reset(struct scsi_cmnd *sc_cmd);
  802. /*
  803. * Reset the host adapter.
  804. */
  805. int fc_eh_host_reset(struct scsi_cmnd *sc_cmd);
  806. /*
  807. * Check rport status.
  808. */
  809. int fc_slave_alloc(struct scsi_device *sdev);
  810. /*
  811. * Adjust the queue depth.
  812. */
  813. int fc_change_queue_depth(struct scsi_device *sdev, int qdepth);
  814. /*
  815. * Change the tag type.
  816. */
  817. int fc_change_queue_type(struct scsi_device *sdev, int tag_type);
  818. /*
  819. * Free memory pools used by the FCP layer.
  820. */
  821. void fc_fcp_destroy(struct fc_lport *);
  822. /*
  823. * Set up direct-data placement for this I/O request
  824. */
  825. void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid);
  826. /*
  827. * ELS/CT interface
  828. *****************************/
  829. /*
  830. * Initializes ELS/CT interface
  831. */
  832. int fc_elsct_init(struct fc_lport *lp);
  833. /*
  834. * EXCHANGE MANAGER LAYER
  835. *****************************/
  836. /*
  837. * Initializes Exchange Manager related
  838. * function pointers in struct libfc_function_template.
  839. */
  840. int fc_exch_init(struct fc_lport *lp);
  841. /*
  842. * Adds Exchange Manager (EM) mp to lport.
  843. *
  844. * Adds specified mp to lport using struct fc_exch_mgr_anchor,
  845. * the struct fc_exch_mgr_anchor allows same EM sharing by
  846. * more than one lport with their specified match function,
  847. * the match function is used in allocating exchange from
  848. * added mp.
  849. */
  850. struct fc_exch_mgr_anchor *fc_exch_mgr_add(struct fc_lport *lport,
  851. struct fc_exch_mgr *mp,
  852. bool (*match)(struct fc_frame *));
  853. /*
  854. * Deletes Exchange Manager (EM) from lport by removing
  855. * its anchor ema from lport.
  856. *
  857. * If removed anchor ema was the last user of its associated EM
  858. * then also destroys associated EM.
  859. */
  860. void fc_exch_mgr_del(struct fc_exch_mgr_anchor *ema);
  861. /*
  862. * Allocates an Exchange Manager (EM).
  863. *
  864. * The EM manages exchanges for their allocation and
  865. * free, also allows exchange lookup for received
  866. * frame.
  867. *
  868. * The class is used for initializing FC class of
  869. * allocated exchange from EM.
  870. *
  871. * The min_xid and max_xid will limit new
  872. * exchange ID (XID) within this range for
  873. * a new exchange.
  874. * The LLD may choose to have multiple EMs,
  875. * e.g. one EM instance per CPU receive thread in LLD.
  876. *
  877. * Specified match function is used in allocating exchanges
  878. * from newly allocated EM.
  879. */
  880. struct fc_exch_mgr *fc_exch_mgr_alloc(struct fc_lport *lp,
  881. enum fc_class class,
  882. u16 min_xid,
  883. u16 max_xid,
  884. bool (*match)(struct fc_frame *));
  885. /*
  886. * Free all exchange managers of a lport.
  887. */
  888. void fc_exch_mgr_free(struct fc_lport *lport);
  889. /*
  890. * Receive a frame on specified local port and exchange manager.
  891. */
  892. void fc_exch_recv(struct fc_lport *lp, struct fc_frame *fp);
  893. /*
  894. * This function is for exch_seq_send function pointer in
  895. * struct libfc_function_template, see comment block on
  896. * exch_seq_send for description of this function.
  897. */
  898. struct fc_seq *fc_exch_seq_send(struct fc_lport *lp,
  899. struct fc_frame *fp,
  900. void (*resp)(struct fc_seq *sp,
  901. struct fc_frame *fp,
  902. void *arg),
  903. void (*destructor)(struct fc_seq *sp,
  904. void *arg),
  905. void *arg, u32 timer_msec);
  906. /*
  907. * send a frame using existing sequence and exchange.
  908. */
  909. int fc_seq_send(struct fc_lport *lp, struct fc_seq *sp, struct fc_frame *fp);
  910. /*
  911. * Send ELS response using mainly infomation
  912. * in exchange and sequence in EM layer.
  913. */
  914. void fc_seq_els_rsp_send(struct fc_seq *sp, enum fc_els_cmd els_cmd,
  915. struct fc_seq_els_data *els_data);
  916. /*
  917. * This function is for seq_exch_abort function pointer in
  918. * struct libfc_function_template, see comment block on
  919. * seq_exch_abort for description of this function.
  920. */
  921. int fc_seq_exch_abort(const struct fc_seq *req_sp, unsigned int timer_msec);
  922. /*
  923. * Indicate that an exchange/sequence tuple is complete and the memory
  924. * allocated for the related objects may be freed.
  925. */
  926. void fc_exch_done(struct fc_seq *sp);
  927. /*
  928. * Allocate a new exchange and sequence pair.
  929. */
  930. struct fc_exch *fc_exch_alloc(struct fc_lport *lport, struct fc_frame *fp);
  931. /*
  932. * Start a new sequence on the same exchange as the supplied sequence.
  933. */
  934. struct fc_seq *fc_seq_start_next(struct fc_seq *sp);
  935. /*
  936. * Reset all EMs of a lport, releasing its all sequences and
  937. * exchanges. If sid is non-zero, then reset only exchanges
  938. * we sourced from that FID. If did is non-zero, reset only
  939. * exchanges destined to that FID.
  940. */
  941. void fc_exch_mgr_reset(struct fc_lport *, u32 s_id, u32 d_id);
  942. /*
  943. * Functions for fc_functions_template
  944. */
  945. void fc_get_host_speed(struct Scsi_Host *shost);
  946. void fc_get_host_port_type(struct Scsi_Host *shost);
  947. void fc_get_host_port_state(struct Scsi_Host *shost);
  948. void fc_set_rport_loss_tmo(struct fc_rport *rport, u32 timeout);
  949. struct fc_host_statistics *fc_get_host_stats(struct Scsi_Host *);
  950. /*
  951. * module setup functions.
  952. */
  953. int fc_setup_exch_mgr(void);
  954. void fc_destroy_exch_mgr(void);
  955. int fc_setup_rport(void);
  956. void fc_destroy_rport(void);
  957. /*
  958. * Internal libfc functions.
  959. */
  960. const char *fc_els_resp_type(struct fc_frame *);
  961. #endif /* _LIBFC_H_ */