fc_fcp.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright(c) 2007 Intel Corporation. All rights reserved.
  4. * Copyright(c) 2008 Red Hat, Inc. All rights reserved.
  5. * Copyright(c) 2008 Mike Christie
  6. *
  7. * Maintained at www.Open-FCoE.org
  8. */
  9. #include <linux/module.h>
  10. #include <linux/delay.h>
  11. #include <linux/kernel.h>
  12. #include <linux/types.h>
  13. #include <linux/spinlock.h>
  14. #include <linux/scatterlist.h>
  15. #include <linux/err.h>
  16. #include <linux/crc32.h>
  17. #include <linux/slab.h>
  18. #include <scsi/scsi_tcq.h>
  19. #include <scsi/scsi.h>
  20. #include <scsi/scsi_host.h>
  21. #include <scsi/scsi_device.h>
  22. #include <scsi/scsi_cmnd.h>
  23. #include <scsi/fc/fc_fc2.h>
  24. #include <scsi/libfc.h>
  25. #include "fc_encode.h"
  26. #include "fc_libfc.h"
  27. static struct kmem_cache *scsi_pkt_cachep;
  28. /* SRB state definitions */
  29. #define FC_SRB_FREE 0 /* cmd is free */
  30. #define FC_SRB_CMD_SENT (1 << 0) /* cmd has been sent */
  31. #define FC_SRB_RCV_STATUS (1 << 1) /* response has arrived */
  32. #define FC_SRB_ABORT_PENDING (1 << 2) /* cmd abort sent to device */
  33. #define FC_SRB_ABORTED (1 << 3) /* abort acknowledged */
  34. #define FC_SRB_DISCONTIG (1 << 4) /* non-sequential data recvd */
  35. #define FC_SRB_COMPL (1 << 5) /* fc_io_compl has been run */
  36. #define FC_SRB_FCP_PROCESSING_TMO (1 << 6) /* timer function processing */
  37. #define FC_SRB_READ (1 << 1)
  38. #define FC_SRB_WRITE (1 << 0)
  39. static struct libfc_cmd_priv *libfc_priv(struct scsi_cmnd *cmd)
  40. {
  41. return scsi_cmd_priv(cmd);
  42. }
  43. /**
  44. * struct fc_fcp_internal - FCP layer internal data
  45. * @scsi_pkt_pool: Memory pool to draw FCP packets from
  46. * @scsi_queue_lock: Protects the scsi_pkt_queue
  47. * @scsi_pkt_queue: Current FCP packets
  48. * @last_can_queue_ramp_down_time: ramp down time
  49. * @last_can_queue_ramp_up_time: ramp up time
  50. * @max_can_queue: max can_queue size
  51. */
  52. struct fc_fcp_internal {
  53. mempool_t *scsi_pkt_pool;
  54. spinlock_t scsi_queue_lock;
  55. struct list_head scsi_pkt_queue;
  56. unsigned long last_can_queue_ramp_down_time;
  57. unsigned long last_can_queue_ramp_up_time;
  58. int max_can_queue;
  59. };
  60. #define fc_get_scsi_internal(x) ((struct fc_fcp_internal *)(x)->scsi_priv)
  61. /*
  62. * function prototypes
  63. * FC scsi I/O related functions
  64. */
  65. static void fc_fcp_recv_data(struct fc_fcp_pkt *, struct fc_frame *);
  66. static void fc_fcp_recv(struct fc_seq *, struct fc_frame *, void *);
  67. static void fc_fcp_resp(struct fc_fcp_pkt *, struct fc_frame *);
  68. static void fc_fcp_complete_locked(struct fc_fcp_pkt *);
  69. static void fc_tm_done(struct fc_seq *, struct fc_frame *, void *);
  70. static void fc_fcp_error(struct fc_fcp_pkt *, struct fc_frame *);
  71. static void fc_fcp_recovery(struct fc_fcp_pkt *, u8 code);
  72. static void fc_fcp_timeout(struct timer_list *);
  73. static void fc_fcp_rec(struct fc_fcp_pkt *);
  74. static void fc_fcp_rec_error(struct fc_fcp_pkt *, struct fc_frame *);
  75. static void fc_fcp_rec_resp(struct fc_seq *, struct fc_frame *, void *);
  76. static void fc_io_compl(struct fc_fcp_pkt *);
  77. static void fc_fcp_srr(struct fc_fcp_pkt *, enum fc_rctl, u32);
  78. static void fc_fcp_srr_resp(struct fc_seq *, struct fc_frame *, void *);
  79. static void fc_fcp_srr_error(struct fc_fcp_pkt *, struct fc_frame *);
  80. /*
  81. * command status codes
  82. */
  83. #define FC_COMPLETE 0
  84. #define FC_CMD_ABORTED 1
  85. #define FC_CMD_RESET 2
  86. #define FC_CMD_PLOGO 3
  87. #define FC_SNS_RCV 4
  88. #define FC_TRANS_ERR 5
  89. #define FC_DATA_OVRRUN 6
  90. #define FC_DATA_UNDRUN 7
  91. #define FC_ERROR 8
  92. #define FC_HRD_ERROR 9
  93. #define FC_CRC_ERROR 10
  94. #define FC_TIMED_OUT 11
  95. #define FC_TRANS_RESET 12
  96. /*
  97. * Error recovery timeout values.
  98. */
  99. #define FC_SCSI_TM_TOV (10 * HZ)
  100. #define FC_HOST_RESET_TIMEOUT (30 * HZ)
  101. #define FC_CAN_QUEUE_PERIOD (60 * HZ)
  102. #define FC_MAX_ERROR_CNT 5
  103. #define FC_MAX_RECOV_RETRY 3
  104. #define FC_FCP_DFLT_QUEUE_DEPTH 32
  105. /**
  106. * fc_fcp_pkt_alloc() - Allocate a fcp_pkt
  107. * @lport: The local port that the FCP packet is for
  108. * @gfp: GFP flags for allocation
  109. *
  110. * Return value: fcp_pkt structure or null on allocation failure.
  111. * Context: Can be called from process context, no lock is required.
  112. */
  113. static struct fc_fcp_pkt *fc_fcp_pkt_alloc(struct fc_lport *lport, gfp_t gfp)
  114. {
  115. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  116. struct fc_fcp_pkt *fsp;
  117. fsp = mempool_alloc(si->scsi_pkt_pool, gfp);
  118. if (fsp) {
  119. memset(fsp, 0, sizeof(*fsp));
  120. fsp->lp = lport;
  121. fsp->xfer_ddp = FC_XID_UNKNOWN;
  122. refcount_set(&fsp->ref_cnt, 1);
  123. timer_setup(&fsp->timer, NULL, 0);
  124. INIT_LIST_HEAD(&fsp->list);
  125. spin_lock_init(&fsp->scsi_pkt_lock);
  126. } else {
  127. this_cpu_inc(lport->stats->FcpPktAllocFails);
  128. }
  129. return fsp;
  130. }
  131. /**
  132. * fc_fcp_pkt_release() - Release hold on a fcp_pkt
  133. * @fsp: The FCP packet to be released
  134. *
  135. * Context: Can be called from process or interrupt context,
  136. * no lock is required.
  137. */
  138. static void fc_fcp_pkt_release(struct fc_fcp_pkt *fsp)
  139. {
  140. if (refcount_dec_and_test(&fsp->ref_cnt)) {
  141. struct fc_fcp_internal *si = fc_get_scsi_internal(fsp->lp);
  142. mempool_free(fsp, si->scsi_pkt_pool);
  143. }
  144. }
  145. /**
  146. * fc_fcp_pkt_hold() - Hold a fcp_pkt
  147. * @fsp: The FCP packet to be held
  148. */
  149. static void fc_fcp_pkt_hold(struct fc_fcp_pkt *fsp)
  150. {
  151. refcount_inc(&fsp->ref_cnt);
  152. }
  153. /**
  154. * fc_fcp_pkt_destroy() - Release hold on a fcp_pkt
  155. * @seq: The sequence that the FCP packet is on (required by destructor API)
  156. * @fsp: The FCP packet to be released
  157. *
  158. * This routine is called by a destructor callback in the fc_exch_seq_send()
  159. * routine of the libfc Transport Template. The 'struct fc_seq' is a required
  160. * argument even though it is not used by this routine.
  161. *
  162. * Context: No locking required.
  163. */
  164. static void fc_fcp_pkt_destroy(struct fc_seq *seq, void *fsp)
  165. {
  166. fc_fcp_pkt_release(fsp);
  167. }
  168. /**
  169. * fc_fcp_lock_pkt() - Lock a fcp_pkt and increase its reference count
  170. * @fsp: The FCP packet to be locked and incremented
  171. *
  172. * We should only return error if we return a command to SCSI-ml before
  173. * getting a response. This can happen in cases where we send a abort, but
  174. * do not wait for the response and the abort and command can be passing
  175. * each other on the wire/network-layer.
  176. *
  177. * Note: this function locks the packet and gets a reference to allow
  178. * callers to call the completion function while the lock is held and
  179. * not have to worry about the packets refcount.
  180. *
  181. * TODO: Maybe we should just have callers grab/release the lock and
  182. * have a function that they call to verify the fsp and grab a ref if
  183. * needed.
  184. */
  185. static inline int fc_fcp_lock_pkt(struct fc_fcp_pkt *fsp)
  186. {
  187. spin_lock_bh(&fsp->scsi_pkt_lock);
  188. if (fsp->state & FC_SRB_COMPL) {
  189. spin_unlock_bh(&fsp->scsi_pkt_lock);
  190. return -EPERM;
  191. }
  192. fc_fcp_pkt_hold(fsp);
  193. return 0;
  194. }
  195. /**
  196. * fc_fcp_unlock_pkt() - Release a fcp_pkt's lock and decrement its
  197. * reference count
  198. * @fsp: The FCP packet to be unlocked and decremented
  199. */
  200. static inline void fc_fcp_unlock_pkt(struct fc_fcp_pkt *fsp)
  201. {
  202. spin_unlock_bh(&fsp->scsi_pkt_lock);
  203. fc_fcp_pkt_release(fsp);
  204. }
  205. /**
  206. * fc_fcp_timer_set() - Start a timer for a fcp_pkt
  207. * @fsp: The FCP packet to start a timer for
  208. * @delay: The timeout period in jiffies
  209. */
  210. static void fc_fcp_timer_set(struct fc_fcp_pkt *fsp, unsigned long delay)
  211. {
  212. if (!(fsp->state & FC_SRB_COMPL)) {
  213. mod_timer(&fsp->timer, jiffies + delay);
  214. fsp->timer_delay = delay;
  215. }
  216. }
  217. static void fc_fcp_abort_done(struct fc_fcp_pkt *fsp)
  218. {
  219. fsp->state |= FC_SRB_ABORTED;
  220. fsp->state &= ~FC_SRB_ABORT_PENDING;
  221. if (fsp->wait_for_comp)
  222. complete(&fsp->tm_done);
  223. else
  224. fc_fcp_complete_locked(fsp);
  225. }
  226. /**
  227. * fc_fcp_send_abort() - Send an abort for exchanges associated with a
  228. * fcp_pkt
  229. * @fsp: The FCP packet to abort exchanges on
  230. */
  231. static int fc_fcp_send_abort(struct fc_fcp_pkt *fsp)
  232. {
  233. int rc;
  234. if (!fsp->seq_ptr)
  235. return -EINVAL;
  236. this_cpu_inc(fsp->lp->stats->FcpPktAborts);
  237. fsp->state |= FC_SRB_ABORT_PENDING;
  238. rc = fc_seq_exch_abort(fsp->seq_ptr, 0);
  239. /*
  240. * fc_seq_exch_abort() might return -ENXIO if
  241. * the sequence is already completed
  242. */
  243. if (rc == -ENXIO) {
  244. fc_fcp_abort_done(fsp);
  245. rc = 0;
  246. }
  247. return rc;
  248. }
  249. /**
  250. * fc_fcp_retry_cmd() - Retry a fcp_pkt
  251. * @fsp: The FCP packet to be retried
  252. * @status_code: The FCP status code to set
  253. *
  254. * Sets the status code to be FC_ERROR and then calls
  255. * fc_fcp_complete_locked() which in turn calls fc_io_compl().
  256. * fc_io_compl() will notify the SCSI-ml that the I/O is done.
  257. * The SCSI-ml will retry the command.
  258. */
  259. static void fc_fcp_retry_cmd(struct fc_fcp_pkt *fsp, int status_code)
  260. {
  261. if (fsp->seq_ptr) {
  262. fc_exch_done(fsp->seq_ptr);
  263. fsp->seq_ptr = NULL;
  264. }
  265. fsp->state &= ~FC_SRB_ABORT_PENDING;
  266. fsp->io_status = 0;
  267. fsp->status_code = status_code;
  268. fc_fcp_complete_locked(fsp);
  269. }
  270. /**
  271. * fc_fcp_ddp_setup() - Calls a LLD's ddp_setup routine to set up DDP context
  272. * @fsp: The FCP packet that will manage the DDP frames
  273. * @xid: The XID that will be used for the DDP exchange
  274. */
  275. void fc_fcp_ddp_setup(struct fc_fcp_pkt *fsp, u16 xid)
  276. {
  277. struct fc_lport *lport;
  278. lport = fsp->lp;
  279. if ((fsp->req_flags & FC_SRB_READ) &&
  280. (lport->lro_enabled) && (lport->tt.ddp_setup)) {
  281. if (lport->tt.ddp_setup(lport, xid, scsi_sglist(fsp->cmd),
  282. scsi_sg_count(fsp->cmd)))
  283. fsp->xfer_ddp = xid;
  284. }
  285. }
  286. /**
  287. * fc_fcp_ddp_done() - Calls a LLD's ddp_done routine to release any
  288. * DDP related resources for a fcp_pkt
  289. * @fsp: The FCP packet that DDP had been used on
  290. */
  291. void fc_fcp_ddp_done(struct fc_fcp_pkt *fsp)
  292. {
  293. struct fc_lport *lport;
  294. if (!fsp)
  295. return;
  296. if (fsp->xfer_ddp == FC_XID_UNKNOWN)
  297. return;
  298. lport = fsp->lp;
  299. if (lport->tt.ddp_done) {
  300. fsp->xfer_len = lport->tt.ddp_done(lport, fsp->xfer_ddp);
  301. fsp->xfer_ddp = FC_XID_UNKNOWN;
  302. }
  303. }
  304. /**
  305. * fc_fcp_can_queue_ramp_up() - increases can_queue
  306. * @lport: lport to ramp up can_queue
  307. */
  308. static void fc_fcp_can_queue_ramp_up(struct fc_lport *lport)
  309. {
  310. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  311. unsigned long flags;
  312. int can_queue;
  313. spin_lock_irqsave(lport->host->host_lock, flags);
  314. if (si->last_can_queue_ramp_up_time &&
  315. (time_before(jiffies, si->last_can_queue_ramp_up_time +
  316. FC_CAN_QUEUE_PERIOD)))
  317. goto unlock;
  318. if (time_before(jiffies, si->last_can_queue_ramp_down_time +
  319. FC_CAN_QUEUE_PERIOD))
  320. goto unlock;
  321. si->last_can_queue_ramp_up_time = jiffies;
  322. can_queue = lport->host->can_queue << 1;
  323. if (can_queue >= si->max_can_queue) {
  324. can_queue = si->max_can_queue;
  325. si->last_can_queue_ramp_down_time = 0;
  326. }
  327. lport->host->can_queue = can_queue;
  328. shost_printk(KERN_ERR, lport->host, "libfc: increased "
  329. "can_queue to %d.\n", can_queue);
  330. unlock:
  331. spin_unlock_irqrestore(lport->host->host_lock, flags);
  332. }
  333. /**
  334. * fc_fcp_can_queue_ramp_down() - reduces can_queue
  335. * @lport: lport to reduce can_queue
  336. *
  337. * If we are getting memory allocation failures, then we may
  338. * be trying to execute too many commands. We let the running
  339. * commands complete or timeout, then try again with a reduced
  340. * can_queue. Eventually we will hit the point where we run
  341. * on all reserved structs.
  342. */
  343. static bool fc_fcp_can_queue_ramp_down(struct fc_lport *lport)
  344. {
  345. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  346. unsigned long flags;
  347. int can_queue;
  348. bool changed = false;
  349. spin_lock_irqsave(lport->host->host_lock, flags);
  350. if (si->last_can_queue_ramp_down_time &&
  351. (time_before(jiffies, si->last_can_queue_ramp_down_time +
  352. FC_CAN_QUEUE_PERIOD)))
  353. goto unlock;
  354. si->last_can_queue_ramp_down_time = jiffies;
  355. can_queue = lport->host->can_queue;
  356. can_queue >>= 1;
  357. if (!can_queue)
  358. can_queue = 1;
  359. lport->host->can_queue = can_queue;
  360. changed = true;
  361. unlock:
  362. spin_unlock_irqrestore(lport->host->host_lock, flags);
  363. return changed;
  364. }
  365. /*
  366. * fc_fcp_frame_alloc() - Allocates fc_frame structure and buffer.
  367. * @lport: fc lport struct
  368. * @len: payload length
  369. *
  370. * Allocates fc_frame structure and buffer but if fails to allocate
  371. * then reduce can_queue.
  372. */
  373. static inline struct fc_frame *fc_fcp_frame_alloc(struct fc_lport *lport,
  374. size_t len)
  375. {
  376. struct fc_frame *fp;
  377. fp = fc_frame_alloc(lport, len);
  378. if (likely(fp))
  379. return fp;
  380. this_cpu_inc(lport->stats->FcpFrameAllocFails);
  381. /* error case */
  382. fc_fcp_can_queue_ramp_down(lport);
  383. shost_printk(KERN_ERR, lport->host,
  384. "libfc: Could not allocate frame, "
  385. "reducing can_queue to %d.\n", lport->host->can_queue);
  386. return NULL;
  387. }
  388. /**
  389. * get_fsp_rec_tov() - Helper function to get REC_TOV
  390. * @fsp: the FCP packet
  391. *
  392. * Returns rec tov in jiffies as rpriv->e_d_tov + 1 second
  393. */
  394. static inline unsigned int get_fsp_rec_tov(struct fc_fcp_pkt *fsp)
  395. {
  396. struct fc_rport_libfc_priv *rpriv = fsp->rport->dd_data;
  397. unsigned int e_d_tov = FC_DEF_E_D_TOV;
  398. if (rpriv && rpriv->e_d_tov > e_d_tov)
  399. e_d_tov = rpriv->e_d_tov;
  400. return msecs_to_jiffies(e_d_tov) + HZ;
  401. }
  402. /**
  403. * fc_fcp_recv_data() - Handler for receiving SCSI-FCP data from a target
  404. * @fsp: The FCP packet the data is on
  405. * @fp: The data frame
  406. */
  407. static void fc_fcp_recv_data(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  408. {
  409. struct scsi_cmnd *sc = fsp->cmd;
  410. struct fc_lport *lport = fsp->lp;
  411. struct fc_frame_header *fh;
  412. size_t start_offset;
  413. size_t offset;
  414. u32 crc;
  415. u32 copy_len = 0;
  416. size_t len;
  417. void *buf;
  418. struct scatterlist *sg;
  419. u32 nents;
  420. u8 host_bcode = FC_COMPLETE;
  421. fh = fc_frame_header_get(fp);
  422. offset = ntohl(fh->fh_parm_offset);
  423. start_offset = offset;
  424. len = fr_len(fp) - sizeof(*fh);
  425. buf = fc_frame_payload_get(fp, 0);
  426. /*
  427. * if this I/O is ddped then clear it and initiate recovery since data
  428. * frames are expected to be placed directly in that case.
  429. *
  430. * Indicate error to scsi-ml because something went wrong with the
  431. * ddp handling to get us here.
  432. */
  433. if (fsp->xfer_ddp != FC_XID_UNKNOWN) {
  434. fc_fcp_ddp_done(fsp);
  435. FC_FCP_DBG(fsp, "DDP I/O in fc_fcp_recv_data set ERROR\n");
  436. host_bcode = FC_ERROR;
  437. goto err;
  438. }
  439. if (offset + len > fsp->data_len) {
  440. /* this should never happen */
  441. if ((fr_flags(fp) & FCPHF_CRC_UNCHECKED) &&
  442. fc_frame_crc_check(fp))
  443. goto crc_err;
  444. FC_FCP_DBG(fsp, "data received past end. len %zx offset %zx "
  445. "data_len %x\n", len, offset, fsp->data_len);
  446. /* Data is corrupted indicate scsi-ml should retry */
  447. host_bcode = FC_DATA_OVRRUN;
  448. goto err;
  449. }
  450. if (offset != fsp->xfer_len)
  451. fsp->state |= FC_SRB_DISCONTIG;
  452. sg = scsi_sglist(sc);
  453. nents = scsi_sg_count(sc);
  454. if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED)) {
  455. copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
  456. &offset, NULL);
  457. } else {
  458. crc = crc32(~0, (u8 *) fh, sizeof(*fh));
  459. copy_len = fc_copy_buffer_to_sglist(buf, len, sg, &nents,
  460. &offset, &crc);
  461. buf = fc_frame_payload_get(fp, 0);
  462. if (len % 4)
  463. crc = crc32(crc, buf + len, 4 - (len % 4));
  464. if (~crc != le32_to_cpu(fr_crc(fp))) {
  465. crc_err:
  466. this_cpu_inc(lport->stats->ErrorFrames);
  467. /* per cpu count, not total count, but OK for limit */
  468. if (this_cpu_inc_return(lport->stats->InvalidCRCCount) < FC_MAX_ERROR_CNT)
  469. printk(KERN_WARNING "libfc: CRC error on data "
  470. "frame for port (%6.6x)\n",
  471. lport->port_id);
  472. /*
  473. * Assume the frame is total garbage.
  474. * We may have copied it over the good part
  475. * of the buffer.
  476. * If so, we need to retry the entire operation.
  477. * Otherwise, ignore it.
  478. */
  479. if (fsp->state & FC_SRB_DISCONTIG) {
  480. host_bcode = FC_CRC_ERROR;
  481. goto err;
  482. }
  483. return;
  484. }
  485. }
  486. if (fsp->xfer_contig_end == start_offset)
  487. fsp->xfer_contig_end += copy_len;
  488. fsp->xfer_len += copy_len;
  489. /*
  490. * In the very rare event that this data arrived after the response
  491. * and completes the transfer, call the completion handler.
  492. */
  493. if (unlikely(fsp->state & FC_SRB_RCV_STATUS) &&
  494. fsp->xfer_len == fsp->data_len - fsp->scsi_resid) {
  495. FC_FCP_DBG( fsp, "complete out-of-order sequence\n" );
  496. fc_fcp_complete_locked(fsp);
  497. }
  498. return;
  499. err:
  500. fc_fcp_recovery(fsp, host_bcode);
  501. }
  502. /**
  503. * fc_fcp_send_data() - Send SCSI data to a target
  504. * @fsp: The FCP packet the data is on
  505. * @seq: The sequence the data is to be sent on
  506. * @offset: The starting offset for this data request
  507. * @seq_blen: The burst length for this data request
  508. *
  509. * Called after receiving a Transfer Ready data descriptor.
  510. * If the LLD is capable of sequence offload then send down the
  511. * seq_blen amount of data in single frame, otherwise send
  512. * multiple frames of the maximum frame payload supported by
  513. * the target port.
  514. */
  515. static int fc_fcp_send_data(struct fc_fcp_pkt *fsp, struct fc_seq *seq,
  516. size_t offset, size_t seq_blen)
  517. {
  518. struct fc_exch *ep;
  519. struct scsi_cmnd *sc;
  520. struct scatterlist *sg;
  521. struct fc_frame *fp = NULL;
  522. struct fc_lport *lport = fsp->lp;
  523. struct page *page;
  524. size_t remaining;
  525. size_t t_blen;
  526. size_t tlen;
  527. size_t sg_bytes;
  528. size_t frame_offset, fh_parm_offset;
  529. size_t off;
  530. int error;
  531. void *data = NULL;
  532. void *page_addr;
  533. int using_sg = lport->sg_supp;
  534. u32 f_ctl;
  535. WARN_ON(seq_blen <= 0);
  536. if (unlikely(offset + seq_blen > fsp->data_len)) {
  537. /* this should never happen */
  538. FC_FCP_DBG(fsp, "xfer-ready past end. seq_blen %zx "
  539. "offset %zx\n", seq_blen, offset);
  540. fc_fcp_send_abort(fsp);
  541. return 0;
  542. } else if (offset != fsp->xfer_len) {
  543. /* Out of Order Data Request - no problem, but unexpected. */
  544. FC_FCP_DBG(fsp, "xfer-ready non-contiguous. "
  545. "seq_blen %zx offset %zx\n", seq_blen, offset);
  546. }
  547. /*
  548. * if LLD is capable of seq_offload then set transport
  549. * burst length (t_blen) to seq_blen, otherwise set t_blen
  550. * to max FC frame payload previously set in fsp->max_payload.
  551. */
  552. t_blen = fsp->max_payload;
  553. if (lport->seq_offload) {
  554. t_blen = min(seq_blen, (size_t)lport->lso_max);
  555. FC_FCP_DBG(fsp, "fsp=%p:lso:blen=%zx lso_max=0x%x t_blen=%zx\n",
  556. fsp, seq_blen, lport->lso_max, t_blen);
  557. }
  558. if (t_blen > 512)
  559. t_blen &= ~(512 - 1); /* round down to block size */
  560. sc = fsp->cmd;
  561. remaining = seq_blen;
  562. fh_parm_offset = frame_offset = offset;
  563. tlen = 0;
  564. seq = fc_seq_start_next(seq);
  565. f_ctl = FC_FC_REL_OFF;
  566. WARN_ON(!seq);
  567. sg = scsi_sglist(sc);
  568. while (remaining > 0 && sg) {
  569. if (offset >= sg->length) {
  570. offset -= sg->length;
  571. sg = sg_next(sg);
  572. continue;
  573. }
  574. if (!fp) {
  575. tlen = min(t_blen, remaining);
  576. /*
  577. * TODO. Temporary workaround. fc_seq_send() can't
  578. * handle odd lengths in non-linear skbs.
  579. * This will be the final fragment only.
  580. */
  581. if (tlen % 4)
  582. using_sg = 0;
  583. fp = fc_frame_alloc(lport, using_sg ? 0 : tlen);
  584. if (!fp)
  585. return -ENOMEM;
  586. data = fc_frame_header_get(fp) + 1;
  587. fh_parm_offset = frame_offset;
  588. fr_max_payload(fp) = fsp->max_payload;
  589. }
  590. off = offset + sg->offset;
  591. sg_bytes = min(tlen, sg->length - offset);
  592. sg_bytes = min(sg_bytes,
  593. (size_t) (PAGE_SIZE - (off & ~PAGE_MASK)));
  594. page = sg_page(sg) + (off >> PAGE_SHIFT);
  595. if (using_sg) {
  596. get_page(page);
  597. skb_fill_page_desc(fp_skb(fp),
  598. skb_shinfo(fp_skb(fp))->nr_frags,
  599. page, off & ~PAGE_MASK, sg_bytes);
  600. fp_skb(fp)->data_len += sg_bytes;
  601. fr_len(fp) += sg_bytes;
  602. fp_skb(fp)->truesize += PAGE_SIZE;
  603. } else {
  604. /*
  605. * The scatterlist item may be bigger than PAGE_SIZE,
  606. * but we must not cross pages inside the kmap.
  607. */
  608. page_addr = kmap_atomic(page);
  609. memcpy(data, (char *)page_addr + (off & ~PAGE_MASK),
  610. sg_bytes);
  611. kunmap_atomic(page_addr);
  612. data += sg_bytes;
  613. }
  614. offset += sg_bytes;
  615. frame_offset += sg_bytes;
  616. tlen -= sg_bytes;
  617. remaining -= sg_bytes;
  618. if ((skb_shinfo(fp_skb(fp))->nr_frags < FC_FRAME_SG_LEN) &&
  619. (tlen))
  620. continue;
  621. /*
  622. * Send sequence with transfer sequence initiative in case
  623. * this is last FCP frame of the sequence.
  624. */
  625. if (remaining == 0)
  626. f_ctl |= FC_FC_SEQ_INIT | FC_FC_END_SEQ;
  627. ep = fc_seq_exch(seq);
  628. fc_fill_fc_hdr(fp, FC_RCTL_DD_SOL_DATA, ep->did, ep->sid,
  629. FC_TYPE_FCP, f_ctl, fh_parm_offset);
  630. /*
  631. * send fragment using for a sequence.
  632. */
  633. error = fc_seq_send(lport, seq, fp);
  634. if (error) {
  635. WARN_ON(1); /* send error should be rare */
  636. return error;
  637. }
  638. fp = NULL;
  639. }
  640. fsp->xfer_len += seq_blen; /* premature count? */
  641. return 0;
  642. }
  643. /**
  644. * fc_fcp_abts_resp() - Receive an ABTS response
  645. * @fsp: The FCP packet that is being aborted
  646. * @fp: The response frame
  647. */
  648. static void fc_fcp_abts_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  649. {
  650. int ba_done = 1;
  651. struct fc_ba_rjt *brp;
  652. struct fc_frame_header *fh;
  653. fh = fc_frame_header_get(fp);
  654. switch (fh->fh_r_ctl) {
  655. case FC_RCTL_BA_ACC:
  656. break;
  657. case FC_RCTL_BA_RJT:
  658. brp = fc_frame_payload_get(fp, sizeof(*brp));
  659. if (brp && brp->br_reason == FC_BA_RJT_LOG_ERR)
  660. break;
  661. fallthrough;
  662. default:
  663. /*
  664. * we will let the command timeout
  665. * and scsi-ml recover in this case,
  666. * therefore cleared the ba_done flag.
  667. */
  668. ba_done = 0;
  669. }
  670. if (ba_done)
  671. fc_fcp_abort_done(fsp);
  672. }
  673. /**
  674. * fc_fcp_recv() - Receive an FCP frame
  675. * @seq: The sequence the frame is on
  676. * @fp: The received frame
  677. * @arg: The related FCP packet
  678. *
  679. * Context: Called from Soft IRQ context. Can not be called
  680. * holding the FCP packet list lock.
  681. */
  682. static void fc_fcp_recv(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  683. {
  684. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  685. struct fc_lport *lport = fsp->lp;
  686. struct fc_frame_header *fh;
  687. struct fcp_txrdy *dd;
  688. u8 r_ctl;
  689. int rc = 0;
  690. if (IS_ERR(fp)) {
  691. fc_fcp_error(fsp, fp);
  692. return;
  693. }
  694. fh = fc_frame_header_get(fp);
  695. r_ctl = fh->fh_r_ctl;
  696. if (lport->state != LPORT_ST_READY) {
  697. FC_FCP_DBG(fsp, "lport state %d, ignoring r_ctl %x\n",
  698. lport->state, r_ctl);
  699. goto out;
  700. }
  701. if (fc_fcp_lock_pkt(fsp))
  702. goto out;
  703. if (fh->fh_type == FC_TYPE_BLS) {
  704. fc_fcp_abts_resp(fsp, fp);
  705. goto unlock;
  706. }
  707. if (fsp->state & (FC_SRB_ABORTED | FC_SRB_ABORT_PENDING)) {
  708. FC_FCP_DBG(fsp, "command aborted, ignoring r_ctl %x\n", r_ctl);
  709. goto unlock;
  710. }
  711. if (r_ctl == FC_RCTL_DD_DATA_DESC) {
  712. /*
  713. * received XFER RDY from the target
  714. * need to send data to the target
  715. */
  716. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  717. dd = fc_frame_payload_get(fp, sizeof(*dd));
  718. WARN_ON(!dd);
  719. rc = fc_fcp_send_data(fsp, seq,
  720. (size_t) ntohl(dd->ft_data_ro),
  721. (size_t) ntohl(dd->ft_burst_len));
  722. if (!rc)
  723. seq->rec_data = fsp->xfer_len;
  724. } else if (r_ctl == FC_RCTL_DD_SOL_DATA) {
  725. /*
  726. * received a DATA frame
  727. * next we will copy the data to the system buffer
  728. */
  729. WARN_ON(fr_len(fp) < sizeof(*fh)); /* len may be 0 */
  730. fc_fcp_recv_data(fsp, fp);
  731. seq->rec_data = fsp->xfer_contig_end;
  732. } else if (r_ctl == FC_RCTL_DD_CMD_STATUS) {
  733. WARN_ON(fr_flags(fp) & FCPHF_CRC_UNCHECKED);
  734. fc_fcp_resp(fsp, fp);
  735. } else {
  736. FC_FCP_DBG(fsp, "unexpected frame. r_ctl %x\n", r_ctl);
  737. }
  738. unlock:
  739. fc_fcp_unlock_pkt(fsp);
  740. out:
  741. fc_frame_free(fp);
  742. }
  743. /**
  744. * fc_fcp_resp() - Handler for FCP responses
  745. * @fsp: The FCP packet the response is for
  746. * @fp: The response frame
  747. */
  748. static void fc_fcp_resp(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  749. {
  750. struct fc_frame_header *fh;
  751. struct fcp_resp *fc_rp;
  752. struct fcp_resp_ext *rp_ex;
  753. struct fcp_resp_rsp_info *fc_rp_info;
  754. u32 plen;
  755. u32 expected_len;
  756. u32 respl = 0;
  757. u32 snsl = 0;
  758. u8 flags = 0;
  759. plen = fr_len(fp);
  760. fh = (struct fc_frame_header *)fr_hdr(fp);
  761. if (unlikely(plen < sizeof(*fh) + sizeof(*fc_rp)))
  762. goto len_err;
  763. plen -= sizeof(*fh);
  764. fc_rp = (struct fcp_resp *)(fh + 1);
  765. fsp->cdb_status = fc_rp->fr_status;
  766. flags = fc_rp->fr_flags;
  767. fsp->scsi_comp_flags = flags;
  768. expected_len = fsp->data_len;
  769. /* if ddp, update xfer len */
  770. fc_fcp_ddp_done(fsp);
  771. if (unlikely((flags & ~FCP_CONF_REQ) || fc_rp->fr_status)) {
  772. rp_ex = (void *)(fc_rp + 1);
  773. if (flags & (FCP_RSP_LEN_VAL | FCP_SNS_LEN_VAL)) {
  774. if (plen < sizeof(*fc_rp) + sizeof(*rp_ex))
  775. goto len_err;
  776. fc_rp_info = (struct fcp_resp_rsp_info *)(rp_ex + 1);
  777. if (flags & FCP_RSP_LEN_VAL) {
  778. respl = ntohl(rp_ex->fr_rsp_len);
  779. if ((respl != FCP_RESP_RSP_INFO_LEN4) &&
  780. (respl != FCP_RESP_RSP_INFO_LEN8))
  781. goto len_err;
  782. if (fsp->wait_for_comp) {
  783. /* Abuse cdb_status for rsp code */
  784. fsp->cdb_status = fc_rp_info->rsp_code;
  785. complete(&fsp->tm_done);
  786. /*
  787. * tmfs will not have any scsi cmd so
  788. * exit here
  789. */
  790. return;
  791. }
  792. }
  793. if (flags & FCP_SNS_LEN_VAL) {
  794. snsl = ntohl(rp_ex->fr_sns_len);
  795. if (snsl > SCSI_SENSE_BUFFERSIZE)
  796. snsl = SCSI_SENSE_BUFFERSIZE;
  797. memcpy(fsp->cmd->sense_buffer,
  798. (char *)fc_rp_info + respl, snsl);
  799. }
  800. }
  801. if (flags & (FCP_RESID_UNDER | FCP_RESID_OVER)) {
  802. if (plen < sizeof(*fc_rp) + sizeof(rp_ex->fr_resid))
  803. goto len_err;
  804. if (flags & FCP_RESID_UNDER) {
  805. fsp->scsi_resid = ntohl(rp_ex->fr_resid);
  806. /*
  807. * The cmnd->underflow is the minimum number of
  808. * bytes that must be transferred for this
  809. * command. Provided a sense condition is not
  810. * present, make sure the actual amount
  811. * transferred is at least the underflow value
  812. * or fail.
  813. */
  814. if (!(flags & FCP_SNS_LEN_VAL) &&
  815. (fc_rp->fr_status == 0) &&
  816. (scsi_bufflen(fsp->cmd) -
  817. fsp->scsi_resid) < fsp->cmd->underflow)
  818. goto err;
  819. expected_len -= fsp->scsi_resid;
  820. } else {
  821. fsp->status_code = FC_ERROR;
  822. }
  823. }
  824. }
  825. fsp->state |= FC_SRB_RCV_STATUS;
  826. /*
  827. * Check for missing or extra data frames.
  828. */
  829. if (unlikely(fsp->cdb_status == SAM_STAT_GOOD &&
  830. fsp->xfer_len != expected_len)) {
  831. if (fsp->xfer_len < expected_len) {
  832. /*
  833. * Some data may be queued locally,
  834. * Wait a at least one jiffy to see if it is delivered.
  835. * If this expires without data, we may do SRR.
  836. */
  837. if (fsp->lp->qfull) {
  838. FC_FCP_DBG(fsp, "tgt %6.6x queue busy retry\n",
  839. fsp->rport->port_id);
  840. return;
  841. }
  842. FC_FCP_DBG(fsp, "tgt %6.6x xfer len %zx data underrun "
  843. "len %x, data len %x\n",
  844. fsp->rport->port_id,
  845. fsp->xfer_len, expected_len, fsp->data_len);
  846. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  847. return;
  848. }
  849. fsp->status_code = FC_DATA_OVRRUN;
  850. FC_FCP_DBG(fsp, "tgt %6.6x xfer len %zx greater than expected, "
  851. "len %x, data len %x\n",
  852. fsp->rport->port_id,
  853. fsp->xfer_len, expected_len, fsp->data_len);
  854. }
  855. fc_fcp_complete_locked(fsp);
  856. return;
  857. len_err:
  858. FC_FCP_DBG(fsp, "short FCP response. flags 0x%x len %u respl %u "
  859. "snsl %u\n", flags, fr_len(fp), respl, snsl);
  860. err:
  861. fsp->status_code = FC_ERROR;
  862. fc_fcp_complete_locked(fsp);
  863. }
  864. /**
  865. * fc_fcp_complete_locked() - Complete processing of a fcp_pkt with the
  866. * fcp_pkt lock held
  867. * @fsp: The FCP packet to be completed
  868. *
  869. * This function may sleep if a timer is pending. The packet lock must be
  870. * held, and the host lock must not be held.
  871. */
  872. static void fc_fcp_complete_locked(struct fc_fcp_pkt *fsp)
  873. {
  874. struct fc_lport *lport = fsp->lp;
  875. struct fc_seq *seq;
  876. struct fc_exch *ep;
  877. u32 f_ctl;
  878. if (fsp->state & FC_SRB_ABORT_PENDING)
  879. return;
  880. if (fsp->state & FC_SRB_ABORTED) {
  881. if (!fsp->status_code)
  882. fsp->status_code = FC_CMD_ABORTED;
  883. } else {
  884. /*
  885. * Test for transport underrun, independent of response
  886. * underrun status.
  887. */
  888. if (fsp->cdb_status == SAM_STAT_GOOD &&
  889. fsp->xfer_len < fsp->data_len && !fsp->io_status &&
  890. (!(fsp->scsi_comp_flags & FCP_RESID_UNDER) ||
  891. fsp->xfer_len < fsp->data_len - fsp->scsi_resid)) {
  892. FC_FCP_DBG(fsp, "data underrun, xfer %zx data %x\n",
  893. fsp->xfer_len, fsp->data_len);
  894. fsp->status_code = FC_DATA_UNDRUN;
  895. }
  896. }
  897. seq = fsp->seq_ptr;
  898. if (seq) {
  899. fsp->seq_ptr = NULL;
  900. if (unlikely(fsp->scsi_comp_flags & FCP_CONF_REQ)) {
  901. struct fc_frame *conf_frame;
  902. struct fc_seq *csp;
  903. csp = fc_seq_start_next(seq);
  904. conf_frame = fc_fcp_frame_alloc(fsp->lp, 0);
  905. if (conf_frame) {
  906. f_ctl = FC_FC_SEQ_INIT;
  907. f_ctl |= FC_FC_LAST_SEQ | FC_FC_END_SEQ;
  908. ep = fc_seq_exch(seq);
  909. fc_fill_fc_hdr(conf_frame, FC_RCTL_DD_SOL_CTL,
  910. ep->did, ep->sid,
  911. FC_TYPE_FCP, f_ctl, 0);
  912. fc_seq_send(lport, csp, conf_frame);
  913. }
  914. }
  915. fc_exch_done(seq);
  916. }
  917. /*
  918. * Some resets driven by SCSI are not I/Os and do not have
  919. * SCSI commands associated with the requests. We should not
  920. * call I/O completion if we do not have a SCSI command.
  921. */
  922. if (fsp->cmd)
  923. fc_io_compl(fsp);
  924. }
  925. /**
  926. * fc_fcp_cleanup_cmd() - Cancel the active exchange on a fcp_pkt
  927. * @fsp: The FCP packet whose exchanges should be canceled
  928. * @error: The reason for the cancellation
  929. */
  930. static void fc_fcp_cleanup_cmd(struct fc_fcp_pkt *fsp, int error)
  931. {
  932. if (fsp->seq_ptr) {
  933. fc_exch_done(fsp->seq_ptr);
  934. fsp->seq_ptr = NULL;
  935. }
  936. fsp->status_code = error;
  937. }
  938. /**
  939. * fc_fcp_cleanup_each_cmd() - Cancel all exchanges on a local port
  940. * @lport: The local port whose exchanges should be canceled
  941. * @id: The target's ID
  942. * @lun: The LUN
  943. * @error: The reason for cancellation
  944. *
  945. * If lun or id is -1, they are ignored.
  946. */
  947. static void fc_fcp_cleanup_each_cmd(struct fc_lport *lport, unsigned int id,
  948. unsigned int lun, int error)
  949. {
  950. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  951. struct fc_fcp_pkt *fsp;
  952. struct scsi_cmnd *sc_cmd;
  953. unsigned long flags;
  954. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  955. restart:
  956. list_for_each_entry(fsp, &si->scsi_pkt_queue, list) {
  957. sc_cmd = fsp->cmd;
  958. if (id != -1 && scmd_id(sc_cmd) != id)
  959. continue;
  960. if (lun != -1 && sc_cmd->device->lun != lun)
  961. continue;
  962. fc_fcp_pkt_hold(fsp);
  963. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  964. spin_lock_bh(&fsp->scsi_pkt_lock);
  965. if (!(fsp->state & FC_SRB_COMPL)) {
  966. fsp->state |= FC_SRB_COMPL;
  967. /*
  968. * TODO: dropping scsi_pkt_lock and then reacquiring
  969. * again around fc_fcp_cleanup_cmd() is required,
  970. * since fc_fcp_cleanup_cmd() calls into
  971. * fc_seq_set_resp() and that func preempts cpu using
  972. * schedule. May be schedule and related code should be
  973. * removed instead of unlocking here to avoid scheduling
  974. * while atomic bug.
  975. */
  976. spin_unlock_bh(&fsp->scsi_pkt_lock);
  977. fc_fcp_cleanup_cmd(fsp, error);
  978. spin_lock_bh(&fsp->scsi_pkt_lock);
  979. fc_io_compl(fsp);
  980. }
  981. spin_unlock_bh(&fsp->scsi_pkt_lock);
  982. fc_fcp_pkt_release(fsp);
  983. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  984. /*
  985. * while we dropped the lock multiple pkts could
  986. * have been released, so we have to start over.
  987. */
  988. goto restart;
  989. }
  990. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  991. }
  992. /**
  993. * fc_fcp_abort_io() - Abort all FCP-SCSI exchanges on a local port
  994. * @lport: The local port whose exchanges are to be aborted
  995. */
  996. static void fc_fcp_abort_io(struct fc_lport *lport)
  997. {
  998. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_HRD_ERROR);
  999. }
  1000. /**
  1001. * fc_fcp_pkt_send() - Send a fcp_pkt
  1002. * @lport: The local port to send the FCP packet on
  1003. * @fsp: The FCP packet to send
  1004. *
  1005. * Return: Zero for success and -1 for failure
  1006. * Locks: Called without locks held
  1007. */
  1008. static int fc_fcp_pkt_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp)
  1009. {
  1010. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  1011. unsigned long flags;
  1012. int rc;
  1013. libfc_priv(fsp->cmd)->fsp = fsp;
  1014. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  1015. fsp->cdb_cmd.fc_flags = fsp->req_flags & ~FCP_CFL_LEN_MASK;
  1016. int_to_scsilun(fsp->cmd->device->lun, &fsp->cdb_cmd.fc_lun);
  1017. memcpy(fsp->cdb_cmd.fc_cdb, fsp->cmd->cmnd, fsp->cmd->cmd_len);
  1018. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  1019. list_add_tail(&fsp->list, &si->scsi_pkt_queue);
  1020. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1021. rc = lport->tt.fcp_cmd_send(lport, fsp, fc_fcp_recv);
  1022. if (unlikely(rc)) {
  1023. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  1024. libfc_priv(fsp->cmd)->fsp = NULL;
  1025. list_del(&fsp->list);
  1026. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1027. }
  1028. return rc;
  1029. }
  1030. /**
  1031. * fc_fcp_cmd_send() - Send a FCP command
  1032. * @lport: The local port to send the command on
  1033. * @fsp: The FCP packet the command is on
  1034. * @resp: The handler for the response
  1035. */
  1036. static int fc_fcp_cmd_send(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  1037. void (*resp)(struct fc_seq *,
  1038. struct fc_frame *fp,
  1039. void *arg))
  1040. {
  1041. struct fc_frame *fp;
  1042. struct fc_seq *seq;
  1043. struct fc_rport *rport;
  1044. struct fc_rport_libfc_priv *rpriv;
  1045. const size_t len = sizeof(fsp->cdb_cmd);
  1046. int rc = 0;
  1047. if (fc_fcp_lock_pkt(fsp))
  1048. return 0;
  1049. fp = fc_fcp_frame_alloc(lport, sizeof(fsp->cdb_cmd));
  1050. if (!fp) {
  1051. rc = -1;
  1052. goto unlock;
  1053. }
  1054. memcpy(fc_frame_payload_get(fp, len), &fsp->cdb_cmd, len);
  1055. fr_fsp(fp) = fsp;
  1056. rport = fsp->rport;
  1057. fsp->max_payload = rport->maxframe_size;
  1058. rpriv = rport->dd_data;
  1059. fc_fill_fc_hdr(fp, FC_RCTL_DD_UNSOL_CMD, rport->port_id,
  1060. rpriv->local_port->port_id, FC_TYPE_FCP,
  1061. FC_FCTL_REQ, 0);
  1062. seq = fc_exch_seq_send(lport, fp, resp, fc_fcp_pkt_destroy, fsp, 0);
  1063. if (!seq) {
  1064. rc = -1;
  1065. goto unlock;
  1066. }
  1067. fsp->seq_ptr = seq;
  1068. fc_fcp_pkt_hold(fsp); /* hold for fc_fcp_pkt_destroy */
  1069. fsp->timer.function = fc_fcp_timeout;
  1070. if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1071. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  1072. unlock:
  1073. fc_fcp_unlock_pkt(fsp);
  1074. return rc;
  1075. }
  1076. /**
  1077. * fc_fcp_error() - Handler for FCP layer errors
  1078. * @fsp: The FCP packet the error is on
  1079. * @fp: The frame that has errored
  1080. */
  1081. static void fc_fcp_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1082. {
  1083. int error = PTR_ERR(fp);
  1084. if (fc_fcp_lock_pkt(fsp))
  1085. return;
  1086. if (error == -FC_EX_CLOSED) {
  1087. fc_fcp_retry_cmd(fsp, FC_ERROR);
  1088. goto unlock;
  1089. }
  1090. /*
  1091. * clear abort pending, because the lower layer
  1092. * decided to force completion.
  1093. */
  1094. fsp->state &= ~FC_SRB_ABORT_PENDING;
  1095. fsp->status_code = FC_CMD_PLOGO;
  1096. fc_fcp_complete_locked(fsp);
  1097. unlock:
  1098. fc_fcp_unlock_pkt(fsp);
  1099. }
  1100. /**
  1101. * fc_fcp_pkt_abort() - Abort a fcp_pkt
  1102. * @fsp: The FCP packet to abort on
  1103. *
  1104. * Called to send an abort and then wait for abort completion
  1105. */
  1106. static int fc_fcp_pkt_abort(struct fc_fcp_pkt *fsp)
  1107. {
  1108. int rc = FAILED;
  1109. unsigned long ticks_left;
  1110. FC_FCP_DBG(fsp, "pkt abort state %x\n", fsp->state);
  1111. if (fc_fcp_send_abort(fsp)) {
  1112. FC_FCP_DBG(fsp, "failed to send abort\n");
  1113. return FAILED;
  1114. }
  1115. if (fsp->state & FC_SRB_ABORTED) {
  1116. FC_FCP_DBG(fsp, "target abort cmd completed\n");
  1117. return SUCCESS;
  1118. }
  1119. init_completion(&fsp->tm_done);
  1120. fsp->wait_for_comp = 1;
  1121. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1122. ticks_left = wait_for_completion_timeout(&fsp->tm_done,
  1123. FC_SCSI_TM_TOV);
  1124. spin_lock_bh(&fsp->scsi_pkt_lock);
  1125. fsp->wait_for_comp = 0;
  1126. if (!ticks_left) {
  1127. FC_FCP_DBG(fsp, "target abort cmd failed\n");
  1128. } else if (fsp->state & FC_SRB_ABORTED) {
  1129. FC_FCP_DBG(fsp, "target abort cmd passed\n");
  1130. rc = SUCCESS;
  1131. fc_fcp_complete_locked(fsp);
  1132. }
  1133. return rc;
  1134. }
  1135. /**
  1136. * fc_lun_reset_send() - Send LUN reset command
  1137. * @t: Timer context used to fetch the FSP packet
  1138. */
  1139. static void fc_lun_reset_send(struct timer_list *t)
  1140. {
  1141. struct fc_fcp_pkt *fsp = from_timer(fsp, t, timer);
  1142. struct fc_lport *lport = fsp->lp;
  1143. if (lport->tt.fcp_cmd_send(lport, fsp, fc_tm_done)) {
  1144. if (fsp->recov_retry++ >= FC_MAX_RECOV_RETRY)
  1145. return;
  1146. if (fc_fcp_lock_pkt(fsp))
  1147. return;
  1148. fsp->timer.function = fc_lun_reset_send;
  1149. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  1150. fc_fcp_unlock_pkt(fsp);
  1151. }
  1152. }
  1153. /**
  1154. * fc_lun_reset() - Send a LUN RESET command to a device
  1155. * and wait for the reply
  1156. * @lport: The local port to sent the command on
  1157. * @fsp: The FCP packet that identifies the LUN to be reset
  1158. * @id: The SCSI command ID
  1159. * @lun: The LUN ID to be reset
  1160. */
  1161. static int fc_lun_reset(struct fc_lport *lport, struct fc_fcp_pkt *fsp,
  1162. unsigned int id, unsigned int lun)
  1163. {
  1164. int rc;
  1165. fsp->cdb_cmd.fc_dl = htonl(fsp->data_len);
  1166. fsp->cdb_cmd.fc_tm_flags = FCP_TMF_LUN_RESET;
  1167. int_to_scsilun(lun, &fsp->cdb_cmd.fc_lun);
  1168. fsp->wait_for_comp = 1;
  1169. init_completion(&fsp->tm_done);
  1170. fc_lun_reset_send(&fsp->timer);
  1171. /*
  1172. * wait for completion of reset
  1173. * after that make sure all commands are terminated
  1174. */
  1175. rc = wait_for_completion_timeout(&fsp->tm_done, FC_SCSI_TM_TOV);
  1176. spin_lock_bh(&fsp->scsi_pkt_lock);
  1177. fsp->state |= FC_SRB_COMPL;
  1178. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1179. del_timer_sync(&fsp->timer);
  1180. spin_lock_bh(&fsp->scsi_pkt_lock);
  1181. if (fsp->seq_ptr) {
  1182. fc_exch_done(fsp->seq_ptr);
  1183. fsp->seq_ptr = NULL;
  1184. }
  1185. fsp->wait_for_comp = 0;
  1186. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1187. if (!rc) {
  1188. FC_SCSI_DBG(lport, "lun reset failed\n");
  1189. return FAILED;
  1190. }
  1191. /* cdb_status holds the tmf's rsp code */
  1192. if (fsp->cdb_status != FCP_TMF_CMPL)
  1193. return FAILED;
  1194. FC_SCSI_DBG(lport, "lun reset to lun %u completed\n", lun);
  1195. fc_fcp_cleanup_each_cmd(lport, id, lun, FC_CMD_ABORTED);
  1196. return SUCCESS;
  1197. }
  1198. /**
  1199. * fc_tm_done() - Task Management response handler
  1200. * @seq: The sequence that the response is on
  1201. * @fp: The response frame
  1202. * @arg: The FCP packet the response is for
  1203. */
  1204. static void fc_tm_done(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1205. {
  1206. struct fc_fcp_pkt *fsp = arg;
  1207. struct fc_frame_header *fh;
  1208. if (IS_ERR(fp)) {
  1209. /*
  1210. * If there is an error just let it timeout or wait
  1211. * for TMF to be aborted if it timedout.
  1212. *
  1213. * scsi-eh will escalate for when either happens.
  1214. */
  1215. return;
  1216. }
  1217. if (fc_fcp_lock_pkt(fsp))
  1218. goto out;
  1219. /*
  1220. * raced with eh timeout handler.
  1221. */
  1222. if (!fsp->seq_ptr || !fsp->wait_for_comp)
  1223. goto out_unlock;
  1224. fh = fc_frame_header_get(fp);
  1225. if (fh->fh_type != FC_TYPE_BLS)
  1226. fc_fcp_resp(fsp, fp);
  1227. fsp->seq_ptr = NULL;
  1228. fc_exch_done(seq);
  1229. out_unlock:
  1230. fc_fcp_unlock_pkt(fsp);
  1231. out:
  1232. fc_frame_free(fp);
  1233. }
  1234. /**
  1235. * fc_fcp_cleanup() - Cleanup all FCP exchanges on a local port
  1236. * @lport: The local port to be cleaned up
  1237. */
  1238. static void fc_fcp_cleanup(struct fc_lport *lport)
  1239. {
  1240. fc_fcp_cleanup_each_cmd(lport, -1, -1, FC_ERROR);
  1241. }
  1242. /**
  1243. * fc_fcp_timeout() - Handler for fcp_pkt timeouts
  1244. * @t: Timer context used to fetch the FSP packet
  1245. *
  1246. * If REC is supported then just issue it and return. The REC exchange will
  1247. * complete or time out and recovery can continue at that point. Otherwise,
  1248. * if the response has been received without all the data it has been
  1249. * ER_TIMEOUT since the response was received. If the response has not been
  1250. * received we see if data was received recently. If it has been then we
  1251. * continue waiting, otherwise, we abort the command.
  1252. */
  1253. static void fc_fcp_timeout(struct timer_list *t)
  1254. {
  1255. struct fc_fcp_pkt *fsp = from_timer(fsp, t, timer);
  1256. struct fc_rport *rport = fsp->rport;
  1257. struct fc_rport_libfc_priv *rpriv = rport->dd_data;
  1258. if (fc_fcp_lock_pkt(fsp))
  1259. return;
  1260. if (fsp->cdb_cmd.fc_tm_flags)
  1261. goto unlock;
  1262. if (fsp->lp->qfull) {
  1263. FC_FCP_DBG(fsp, "fcp timeout, resetting timer delay %d\n",
  1264. fsp->timer_delay);
  1265. fsp->timer.function = fc_fcp_timeout;
  1266. fc_fcp_timer_set(fsp, fsp->timer_delay);
  1267. goto unlock;
  1268. }
  1269. FC_FCP_DBG(fsp, "fcp timeout, delay %d flags %x state %x\n",
  1270. fsp->timer_delay, rpriv->flags, fsp->state);
  1271. fsp->state |= FC_SRB_FCP_PROCESSING_TMO;
  1272. if (rpriv->flags & FC_RP_FLAGS_REC_SUPPORTED)
  1273. fc_fcp_rec(fsp);
  1274. else if (fsp->state & FC_SRB_RCV_STATUS)
  1275. fc_fcp_complete_locked(fsp);
  1276. else
  1277. fc_fcp_recovery(fsp, FC_TIMED_OUT);
  1278. fsp->state &= ~FC_SRB_FCP_PROCESSING_TMO;
  1279. unlock:
  1280. fc_fcp_unlock_pkt(fsp);
  1281. }
  1282. /**
  1283. * fc_fcp_rec() - Send a REC ELS request
  1284. * @fsp: The FCP packet to send the REC request on
  1285. */
  1286. static void fc_fcp_rec(struct fc_fcp_pkt *fsp)
  1287. {
  1288. struct fc_lport *lport;
  1289. struct fc_frame *fp;
  1290. struct fc_rport *rport;
  1291. struct fc_rport_libfc_priv *rpriv;
  1292. lport = fsp->lp;
  1293. rport = fsp->rport;
  1294. rpriv = rport->dd_data;
  1295. if (!fsp->seq_ptr || rpriv->rp_state != RPORT_ST_READY) {
  1296. fsp->status_code = FC_HRD_ERROR;
  1297. fsp->io_status = 0;
  1298. fc_fcp_complete_locked(fsp);
  1299. return;
  1300. }
  1301. fp = fc_fcp_frame_alloc(lport, sizeof(struct fc_els_rec));
  1302. if (!fp)
  1303. goto retry;
  1304. fr_seq(fp) = fsp->seq_ptr;
  1305. fc_fill_fc_hdr(fp, FC_RCTL_ELS_REQ, rport->port_id,
  1306. rpriv->local_port->port_id, FC_TYPE_ELS,
  1307. FC_FCTL_REQ, 0);
  1308. if (lport->tt.elsct_send(lport, rport->port_id, fp, ELS_REC,
  1309. fc_fcp_rec_resp, fsp,
  1310. 2 * lport->r_a_tov)) {
  1311. fc_fcp_pkt_hold(fsp); /* hold while REC outstanding */
  1312. return;
  1313. }
  1314. retry:
  1315. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1316. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  1317. else
  1318. fc_fcp_recovery(fsp, FC_TIMED_OUT);
  1319. }
  1320. /**
  1321. * fc_fcp_rec_resp() - Handler for REC ELS responses
  1322. * @seq: The sequence the response is on
  1323. * @fp: The response frame
  1324. * @arg: The FCP packet the response is on
  1325. *
  1326. * If the response is a reject then the scsi layer will handle
  1327. * the timeout. If the response is a LS_ACC then if the I/O was not completed
  1328. * set the timeout and return. If the I/O was completed then complete the
  1329. * exchange and tell the SCSI layer.
  1330. */
  1331. static void fc_fcp_rec_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1332. {
  1333. struct fc_fcp_pkt *fsp = (struct fc_fcp_pkt *)arg;
  1334. struct fc_els_rec_acc *recp;
  1335. struct fc_els_ls_rjt *rjt;
  1336. u32 e_stat;
  1337. u8 opcode;
  1338. u32 offset;
  1339. enum dma_data_direction data_dir;
  1340. enum fc_rctl r_ctl;
  1341. struct fc_rport_libfc_priv *rpriv;
  1342. if (IS_ERR(fp)) {
  1343. fc_fcp_rec_error(fsp, fp);
  1344. return;
  1345. }
  1346. if (fc_fcp_lock_pkt(fsp))
  1347. goto out;
  1348. fsp->recov_retry = 0;
  1349. opcode = fc_frame_payload_op(fp);
  1350. if (opcode == ELS_LS_RJT) {
  1351. rjt = fc_frame_payload_get(fp, sizeof(*rjt));
  1352. switch (rjt->er_reason) {
  1353. default:
  1354. FC_FCP_DBG(fsp,
  1355. "device %x invalid REC reject %d/%d\n",
  1356. fsp->rport->port_id, rjt->er_reason,
  1357. rjt->er_explan);
  1358. fallthrough;
  1359. case ELS_RJT_UNSUP:
  1360. FC_FCP_DBG(fsp, "device does not support REC\n");
  1361. rpriv = fsp->rport->dd_data;
  1362. /*
  1363. * if we do not spport RECs or got some bogus
  1364. * reason then resetup timer so we check for
  1365. * making progress.
  1366. */
  1367. rpriv->flags &= ~FC_RP_FLAGS_REC_SUPPORTED;
  1368. break;
  1369. case ELS_RJT_LOGIC:
  1370. case ELS_RJT_UNAB:
  1371. FC_FCP_DBG(fsp, "device %x REC reject %d/%d\n",
  1372. fsp->rport->port_id, rjt->er_reason,
  1373. rjt->er_explan);
  1374. /*
  1375. * If response got lost or is stuck in the
  1376. * queue somewhere we have no idea if and when
  1377. * the response will be received. So quarantine
  1378. * the xid and retry the command.
  1379. */
  1380. if (rjt->er_explan == ELS_EXPL_OXID_RXID) {
  1381. struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
  1382. ep->state |= FC_EX_QUARANTINE;
  1383. fsp->state |= FC_SRB_ABORTED;
  1384. fc_fcp_retry_cmd(fsp, FC_TRANS_RESET);
  1385. break;
  1386. }
  1387. fc_fcp_recovery(fsp, FC_TRANS_RESET);
  1388. break;
  1389. }
  1390. } else if (opcode == ELS_LS_ACC) {
  1391. if (fsp->state & FC_SRB_ABORTED)
  1392. goto unlock_out;
  1393. data_dir = fsp->cmd->sc_data_direction;
  1394. recp = fc_frame_payload_get(fp, sizeof(*recp));
  1395. offset = ntohl(recp->reca_fc4value);
  1396. e_stat = ntohl(recp->reca_e_stat);
  1397. if (e_stat & ESB_ST_COMPLETE) {
  1398. /*
  1399. * The exchange is complete.
  1400. *
  1401. * For output, we must've lost the response.
  1402. * For input, all data must've been sent.
  1403. * We lost may have lost the response
  1404. * (and a confirmation was requested) and maybe
  1405. * some data.
  1406. *
  1407. * If all data received, send SRR
  1408. * asking for response. If partial data received,
  1409. * or gaps, SRR requests data at start of gap.
  1410. * Recovery via SRR relies on in-order-delivery.
  1411. */
  1412. if (data_dir == DMA_TO_DEVICE) {
  1413. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1414. } else if (fsp->xfer_contig_end == offset) {
  1415. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1416. } else {
  1417. offset = fsp->xfer_contig_end;
  1418. r_ctl = FC_RCTL_DD_SOL_DATA;
  1419. }
  1420. fc_fcp_srr(fsp, r_ctl, offset);
  1421. } else if (e_stat & ESB_ST_SEQ_INIT) {
  1422. /*
  1423. * The remote port has the initiative, so just
  1424. * keep waiting for it to complete.
  1425. */
  1426. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  1427. } else {
  1428. /*
  1429. * The exchange is incomplete, we have seq. initiative.
  1430. * Lost response with requested confirmation,
  1431. * lost confirmation, lost transfer ready or
  1432. * lost write data.
  1433. *
  1434. * For output, if not all data was received, ask
  1435. * for transfer ready to be repeated.
  1436. *
  1437. * If we received or sent all the data, send SRR to
  1438. * request response.
  1439. *
  1440. * If we lost a response, we may have lost some read
  1441. * data as well.
  1442. */
  1443. r_ctl = FC_RCTL_DD_SOL_DATA;
  1444. if (data_dir == DMA_TO_DEVICE) {
  1445. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1446. if (offset < fsp->data_len)
  1447. r_ctl = FC_RCTL_DD_DATA_DESC;
  1448. } else if (offset == fsp->xfer_contig_end) {
  1449. r_ctl = FC_RCTL_DD_CMD_STATUS;
  1450. } else if (fsp->xfer_contig_end < offset) {
  1451. offset = fsp->xfer_contig_end;
  1452. }
  1453. fc_fcp_srr(fsp, r_ctl, offset);
  1454. }
  1455. }
  1456. unlock_out:
  1457. fc_fcp_unlock_pkt(fsp);
  1458. out:
  1459. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1460. fc_frame_free(fp);
  1461. }
  1462. /**
  1463. * fc_fcp_rec_error() - Handler for REC errors
  1464. * @fsp: The FCP packet the error is on
  1465. * @fp: The REC frame
  1466. */
  1467. static void fc_fcp_rec_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1468. {
  1469. int error = PTR_ERR(fp);
  1470. if (fc_fcp_lock_pkt(fsp))
  1471. goto out;
  1472. switch (error) {
  1473. case -FC_EX_CLOSED:
  1474. FC_FCP_DBG(fsp, "REC %p fid %6.6x exchange closed\n",
  1475. fsp, fsp->rport->port_id);
  1476. fc_fcp_retry_cmd(fsp, FC_ERROR);
  1477. break;
  1478. default:
  1479. FC_FCP_DBG(fsp, "REC %p fid %6.6x error unexpected error %d\n",
  1480. fsp, fsp->rport->port_id, error);
  1481. fsp->status_code = FC_CMD_PLOGO;
  1482. fallthrough;
  1483. case -FC_EX_TIMEOUT:
  1484. /*
  1485. * Assume REC or LS_ACC was lost.
  1486. * The exchange manager will have aborted REC, so retry.
  1487. */
  1488. FC_FCP_DBG(fsp, "REC %p fid %6.6x exchange timeout retry %d/%d\n",
  1489. fsp, fsp->rport->port_id, fsp->recov_retry,
  1490. FC_MAX_RECOV_RETRY);
  1491. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1492. fc_fcp_rec(fsp);
  1493. else
  1494. fc_fcp_recovery(fsp, FC_ERROR);
  1495. break;
  1496. }
  1497. fc_fcp_unlock_pkt(fsp);
  1498. out:
  1499. fc_fcp_pkt_release(fsp); /* drop hold for outstanding REC */
  1500. }
  1501. /**
  1502. * fc_fcp_recovery() - Handler for fcp_pkt recovery
  1503. * @fsp: The FCP pkt that needs to be aborted
  1504. * @code: The FCP status code to set
  1505. */
  1506. static void fc_fcp_recovery(struct fc_fcp_pkt *fsp, u8 code)
  1507. {
  1508. FC_FCP_DBG(fsp, "start recovery code %x\n", code);
  1509. fsp->status_code = code;
  1510. fsp->cdb_status = 0;
  1511. fsp->io_status = 0;
  1512. /*
  1513. * if this fails then we let the scsi command timer fire and
  1514. * scsi-ml escalate.
  1515. */
  1516. fc_fcp_send_abort(fsp);
  1517. }
  1518. /**
  1519. * fc_fcp_srr() - Send a SRR request (Sequence Retransmission Request)
  1520. * @fsp: The FCP packet the SRR is to be sent on
  1521. * @r_ctl: The R_CTL field for the SRR request
  1522. * @offset: The SRR relative offset
  1523. * This is called after receiving status but insufficient data, or
  1524. * when expecting status but the request has timed out.
  1525. */
  1526. static void fc_fcp_srr(struct fc_fcp_pkt *fsp, enum fc_rctl r_ctl, u32 offset)
  1527. {
  1528. struct fc_lport *lport = fsp->lp;
  1529. struct fc_rport *rport;
  1530. struct fc_rport_libfc_priv *rpriv;
  1531. struct fc_exch *ep = fc_seq_exch(fsp->seq_ptr);
  1532. struct fc_seq *seq;
  1533. struct fcp_srr *srr;
  1534. struct fc_frame *fp;
  1535. rport = fsp->rport;
  1536. rpriv = rport->dd_data;
  1537. if (!(rpriv->flags & FC_RP_FLAGS_RETRY) ||
  1538. rpriv->rp_state != RPORT_ST_READY)
  1539. goto retry; /* shouldn't happen */
  1540. fp = fc_fcp_frame_alloc(lport, sizeof(*srr));
  1541. if (!fp)
  1542. goto retry;
  1543. srr = fc_frame_payload_get(fp, sizeof(*srr));
  1544. memset(srr, 0, sizeof(*srr));
  1545. srr->srr_op = ELS_SRR;
  1546. srr->srr_ox_id = htons(ep->oxid);
  1547. srr->srr_rx_id = htons(ep->rxid);
  1548. srr->srr_r_ctl = r_ctl;
  1549. srr->srr_rel_off = htonl(offset);
  1550. fc_fill_fc_hdr(fp, FC_RCTL_ELS4_REQ, rport->port_id,
  1551. rpriv->local_port->port_id, FC_TYPE_FCP,
  1552. FC_FCTL_REQ, 0);
  1553. seq = fc_exch_seq_send(lport, fp, fc_fcp_srr_resp,
  1554. fc_fcp_pkt_destroy,
  1555. fsp, get_fsp_rec_tov(fsp));
  1556. if (!seq)
  1557. goto retry;
  1558. fsp->recov_seq = seq;
  1559. fsp->xfer_len = offset;
  1560. fsp->xfer_contig_end = offset;
  1561. fsp->state &= ~FC_SRB_RCV_STATUS;
  1562. fc_fcp_pkt_hold(fsp); /* hold for outstanding SRR */
  1563. return;
  1564. retry:
  1565. fc_fcp_retry_cmd(fsp, FC_TRANS_RESET);
  1566. }
  1567. /**
  1568. * fc_fcp_srr_resp() - Handler for SRR response
  1569. * @seq: The sequence the SRR is on
  1570. * @fp: The SRR frame
  1571. * @arg: The FCP packet the SRR is on
  1572. */
  1573. static void fc_fcp_srr_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
  1574. {
  1575. struct fc_fcp_pkt *fsp = arg;
  1576. struct fc_frame_header *fh;
  1577. if (IS_ERR(fp)) {
  1578. fc_fcp_srr_error(fsp, fp);
  1579. return;
  1580. }
  1581. if (fc_fcp_lock_pkt(fsp))
  1582. goto out;
  1583. fh = fc_frame_header_get(fp);
  1584. /*
  1585. * BUG? fc_fcp_srr_error calls fc_exch_done which would release
  1586. * the ep. But if fc_fcp_srr_error had got -FC_EX_TIMEOUT,
  1587. * then fc_exch_timeout would be sending an abort. The fc_exch_done
  1588. * call by fc_fcp_srr_error would prevent fc_exch.c from seeing
  1589. * an abort response though.
  1590. */
  1591. if (fh->fh_type == FC_TYPE_BLS) {
  1592. fc_fcp_unlock_pkt(fsp);
  1593. return;
  1594. }
  1595. switch (fc_frame_payload_op(fp)) {
  1596. case ELS_LS_ACC:
  1597. fsp->recov_retry = 0;
  1598. fc_fcp_timer_set(fsp, get_fsp_rec_tov(fsp));
  1599. break;
  1600. case ELS_LS_RJT:
  1601. default:
  1602. fc_fcp_recovery(fsp, FC_ERROR);
  1603. break;
  1604. }
  1605. fc_fcp_unlock_pkt(fsp);
  1606. out:
  1607. fc_exch_done(seq);
  1608. fc_frame_free(fp);
  1609. }
  1610. /**
  1611. * fc_fcp_srr_error() - Handler for SRR errors
  1612. * @fsp: The FCP packet that the SRR error is on
  1613. * @fp: The SRR frame
  1614. */
  1615. static void fc_fcp_srr_error(struct fc_fcp_pkt *fsp, struct fc_frame *fp)
  1616. {
  1617. if (fc_fcp_lock_pkt(fsp))
  1618. goto out;
  1619. switch (PTR_ERR(fp)) {
  1620. case -FC_EX_TIMEOUT:
  1621. FC_FCP_DBG(fsp, "SRR timeout, retries %d\n", fsp->recov_retry);
  1622. if (fsp->recov_retry++ < FC_MAX_RECOV_RETRY)
  1623. fc_fcp_rec(fsp);
  1624. else
  1625. fc_fcp_recovery(fsp, FC_TIMED_OUT);
  1626. break;
  1627. case -FC_EX_CLOSED: /* e.g., link failure */
  1628. FC_FCP_DBG(fsp, "SRR error, exchange closed\n");
  1629. fallthrough;
  1630. default:
  1631. fc_fcp_retry_cmd(fsp, FC_ERROR);
  1632. break;
  1633. }
  1634. fc_fcp_unlock_pkt(fsp);
  1635. out:
  1636. fc_exch_done(fsp->recov_seq);
  1637. }
  1638. /**
  1639. * fc_fcp_lport_queue_ready() - Determine if the lport and it's queue is ready
  1640. * @lport: The local port to be checked
  1641. */
  1642. static inline int fc_fcp_lport_queue_ready(struct fc_lport *lport)
  1643. {
  1644. /* lock ? */
  1645. return (lport->state == LPORT_ST_READY) &&
  1646. lport->link_up && !lport->qfull;
  1647. }
  1648. /**
  1649. * fc_queuecommand() - The queuecommand function of the SCSI template
  1650. * @shost: The Scsi_Host that the command was issued to
  1651. * @sc_cmd: The scsi_cmnd to be executed
  1652. *
  1653. * This is the i/o strategy routine, called by the SCSI layer.
  1654. */
  1655. int fc_queuecommand(struct Scsi_Host *shost, struct scsi_cmnd *sc_cmd)
  1656. {
  1657. struct fc_lport *lport = shost_priv(shost);
  1658. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1659. struct fc_fcp_pkt *fsp;
  1660. int rval;
  1661. int rc = 0;
  1662. rval = fc_remote_port_chkready(rport);
  1663. if (rval) {
  1664. sc_cmd->result = rval;
  1665. scsi_done(sc_cmd);
  1666. return 0;
  1667. }
  1668. if (!*(struct fc_remote_port **)rport->dd_data) {
  1669. /*
  1670. * rport is transitioning from blocked/deleted to
  1671. * online
  1672. */
  1673. sc_cmd->result = DID_IMM_RETRY << 16;
  1674. scsi_done(sc_cmd);
  1675. goto out;
  1676. }
  1677. if (!fc_fcp_lport_queue_ready(lport)) {
  1678. if (lport->qfull) {
  1679. if (fc_fcp_can_queue_ramp_down(lport))
  1680. shost_printk(KERN_ERR, lport->host,
  1681. "libfc: queue full, "
  1682. "reducing can_queue to %d.\n",
  1683. lport->host->can_queue);
  1684. }
  1685. rc = SCSI_MLQUEUE_HOST_BUSY;
  1686. goto out;
  1687. }
  1688. fsp = fc_fcp_pkt_alloc(lport, GFP_ATOMIC);
  1689. if (fsp == NULL) {
  1690. rc = SCSI_MLQUEUE_HOST_BUSY;
  1691. goto out;
  1692. }
  1693. /*
  1694. * build the libfc request pkt
  1695. */
  1696. fsp->cmd = sc_cmd; /* save the cmd */
  1697. fsp->rport = rport; /* set the remote port ptr */
  1698. /*
  1699. * set up the transfer length
  1700. */
  1701. fsp->data_len = scsi_bufflen(sc_cmd);
  1702. fsp->xfer_len = 0;
  1703. /*
  1704. * setup the data direction
  1705. */
  1706. if (sc_cmd->sc_data_direction == DMA_FROM_DEVICE) {
  1707. fsp->req_flags = FC_SRB_READ;
  1708. this_cpu_inc(lport->stats->InputRequests);
  1709. this_cpu_add(lport->stats->InputBytes, fsp->data_len);
  1710. } else if (sc_cmd->sc_data_direction == DMA_TO_DEVICE) {
  1711. fsp->req_flags = FC_SRB_WRITE;
  1712. this_cpu_inc(lport->stats->OutputRequests);
  1713. this_cpu_add(lport->stats->OutputBytes, fsp->data_len);
  1714. } else {
  1715. fsp->req_flags = 0;
  1716. this_cpu_inc(lport->stats->ControlRequests);
  1717. }
  1718. /*
  1719. * send it to the lower layer
  1720. * if we get -1 return then put the request in the pending
  1721. * queue.
  1722. */
  1723. rval = fc_fcp_pkt_send(lport, fsp);
  1724. if (rval != 0) {
  1725. fsp->state = FC_SRB_FREE;
  1726. fc_fcp_pkt_release(fsp);
  1727. rc = SCSI_MLQUEUE_HOST_BUSY;
  1728. }
  1729. out:
  1730. return rc;
  1731. }
  1732. EXPORT_SYMBOL(fc_queuecommand);
  1733. /**
  1734. * fc_io_compl() - Handle responses for completed commands
  1735. * @fsp: The FCP packet that is complete
  1736. *
  1737. * Translates fcp_pkt errors to a Linux SCSI errors.
  1738. * The fcp packet lock must be held when calling.
  1739. */
  1740. static void fc_io_compl(struct fc_fcp_pkt *fsp)
  1741. {
  1742. struct fc_fcp_internal *si;
  1743. struct scsi_cmnd *sc_cmd;
  1744. struct fc_lport *lport;
  1745. unsigned long flags;
  1746. /* release outstanding ddp context */
  1747. fc_fcp_ddp_done(fsp);
  1748. fsp->state |= FC_SRB_COMPL;
  1749. if (!(fsp->state & FC_SRB_FCP_PROCESSING_TMO)) {
  1750. spin_unlock_bh(&fsp->scsi_pkt_lock);
  1751. del_timer_sync(&fsp->timer);
  1752. spin_lock_bh(&fsp->scsi_pkt_lock);
  1753. }
  1754. lport = fsp->lp;
  1755. si = fc_get_scsi_internal(lport);
  1756. /*
  1757. * if can_queue ramp down is done then try can_queue ramp up
  1758. * since commands are completing now.
  1759. */
  1760. if (si->last_can_queue_ramp_down_time)
  1761. fc_fcp_can_queue_ramp_up(lport);
  1762. sc_cmd = fsp->cmd;
  1763. libfc_priv(sc_cmd)->status = fsp->cdb_status;
  1764. switch (fsp->status_code) {
  1765. case FC_COMPLETE:
  1766. if (fsp->cdb_status == 0) {
  1767. /*
  1768. * good I/O status
  1769. */
  1770. sc_cmd->result = DID_OK << 16;
  1771. if (fsp->scsi_resid)
  1772. libfc_priv(sc_cmd)->resid_len = fsp->scsi_resid;
  1773. } else {
  1774. /*
  1775. * transport level I/O was ok but scsi
  1776. * has non zero status
  1777. */
  1778. sc_cmd->result = (DID_OK << 16) | fsp->cdb_status;
  1779. }
  1780. break;
  1781. case FC_ERROR:
  1782. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1783. "due to FC_ERROR\n");
  1784. sc_cmd->result = DID_ERROR << 16;
  1785. break;
  1786. case FC_DATA_UNDRUN:
  1787. if ((fsp->cdb_status == 0) && !(fsp->req_flags & FC_SRB_READ)) {
  1788. /*
  1789. * scsi status is good but transport level
  1790. * underrun.
  1791. */
  1792. if (fsp->state & FC_SRB_RCV_STATUS) {
  1793. sc_cmd->result = DID_OK << 16;
  1794. } else {
  1795. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml"
  1796. " due to FC_DATA_UNDRUN (trans)\n");
  1797. sc_cmd->result = DID_ERROR << 16;
  1798. }
  1799. } else {
  1800. /*
  1801. * scsi got underrun, this is an error
  1802. */
  1803. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1804. "due to FC_DATA_UNDRUN (scsi)\n");
  1805. libfc_priv(sc_cmd)->resid_len = fsp->scsi_resid;
  1806. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1807. }
  1808. break;
  1809. case FC_DATA_OVRRUN:
  1810. /*
  1811. * overrun is an error
  1812. */
  1813. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1814. "due to FC_DATA_OVRRUN\n");
  1815. sc_cmd->result = (DID_ERROR << 16) | fsp->cdb_status;
  1816. break;
  1817. case FC_CMD_ABORTED:
  1818. if (host_byte(sc_cmd->result) == DID_TIME_OUT)
  1819. FC_FCP_DBG(fsp, "Returning DID_TIME_OUT to scsi-ml "
  1820. "due to FC_CMD_ABORTED\n");
  1821. else {
  1822. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1823. "due to FC_CMD_ABORTED\n");
  1824. set_host_byte(sc_cmd, DID_ERROR);
  1825. }
  1826. sc_cmd->result |= fsp->io_status;
  1827. break;
  1828. case FC_CMD_RESET:
  1829. FC_FCP_DBG(fsp, "Returning DID_RESET to scsi-ml "
  1830. "due to FC_CMD_RESET\n");
  1831. sc_cmd->result = (DID_RESET << 16);
  1832. break;
  1833. case FC_TRANS_RESET:
  1834. FC_FCP_DBG(fsp, "Returning DID_SOFT_ERROR to scsi-ml "
  1835. "due to FC_TRANS_RESET\n");
  1836. sc_cmd->result = (DID_SOFT_ERROR << 16);
  1837. break;
  1838. case FC_HRD_ERROR:
  1839. FC_FCP_DBG(fsp, "Returning DID_NO_CONNECT to scsi-ml "
  1840. "due to FC_HRD_ERROR\n");
  1841. sc_cmd->result = (DID_NO_CONNECT << 16);
  1842. break;
  1843. case FC_CRC_ERROR:
  1844. FC_FCP_DBG(fsp, "Returning DID_PARITY to scsi-ml "
  1845. "due to FC_CRC_ERROR\n");
  1846. sc_cmd->result = (DID_PARITY << 16);
  1847. break;
  1848. case FC_TIMED_OUT:
  1849. FC_FCP_DBG(fsp, "Returning DID_BUS_BUSY to scsi-ml "
  1850. "due to FC_TIMED_OUT\n");
  1851. sc_cmd->result = (DID_BUS_BUSY << 16) | fsp->io_status;
  1852. break;
  1853. default:
  1854. FC_FCP_DBG(fsp, "Returning DID_ERROR to scsi-ml "
  1855. "due to unknown error\n");
  1856. sc_cmd->result = (DID_ERROR << 16);
  1857. break;
  1858. }
  1859. if (lport->state != LPORT_ST_READY && fsp->status_code != FC_COMPLETE)
  1860. sc_cmd->result = (DID_TRANSPORT_DISRUPTED << 16);
  1861. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  1862. list_del(&fsp->list);
  1863. libfc_priv(sc_cmd)->fsp = NULL;
  1864. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1865. scsi_done(sc_cmd);
  1866. /* release ref from initial allocation in queue command */
  1867. fc_fcp_pkt_release(fsp);
  1868. }
  1869. /**
  1870. * fc_eh_abort() - Abort a command
  1871. * @sc_cmd: The SCSI command to abort
  1872. *
  1873. * From SCSI host template.
  1874. * Send an ABTS to the target device and wait for the response.
  1875. */
  1876. int fc_eh_abort(struct scsi_cmnd *sc_cmd)
  1877. {
  1878. struct fc_fcp_pkt *fsp;
  1879. struct fc_lport *lport;
  1880. struct fc_fcp_internal *si;
  1881. int rc = FAILED;
  1882. unsigned long flags;
  1883. int rval;
  1884. rval = fc_block_scsi_eh(sc_cmd);
  1885. if (rval)
  1886. return rval;
  1887. lport = shost_priv(sc_cmd->device->host);
  1888. if (lport->state != LPORT_ST_READY)
  1889. return rc;
  1890. else if (!lport->link_up)
  1891. return rc;
  1892. si = fc_get_scsi_internal(lport);
  1893. spin_lock_irqsave(&si->scsi_queue_lock, flags);
  1894. fsp = libfc_priv(sc_cmd)->fsp;
  1895. if (!fsp) {
  1896. /* command completed while scsi eh was setting up */
  1897. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1898. return SUCCESS;
  1899. }
  1900. /* grab a ref so the fsp and sc_cmd cannot be released from under us */
  1901. fc_fcp_pkt_hold(fsp);
  1902. spin_unlock_irqrestore(&si->scsi_queue_lock, flags);
  1903. if (fc_fcp_lock_pkt(fsp)) {
  1904. /* completed while we were waiting for timer to be deleted */
  1905. rc = SUCCESS;
  1906. goto release_pkt;
  1907. }
  1908. rc = fc_fcp_pkt_abort(fsp);
  1909. fc_fcp_unlock_pkt(fsp);
  1910. release_pkt:
  1911. fc_fcp_pkt_release(fsp);
  1912. return rc;
  1913. }
  1914. EXPORT_SYMBOL(fc_eh_abort);
  1915. /**
  1916. * fc_eh_device_reset() - Reset a single LUN
  1917. * @sc_cmd: The SCSI command which identifies the device whose
  1918. * LUN is to be reset
  1919. *
  1920. * Set from SCSI host template.
  1921. */
  1922. int fc_eh_device_reset(struct scsi_cmnd *sc_cmd)
  1923. {
  1924. struct fc_lport *lport;
  1925. struct fc_fcp_pkt *fsp;
  1926. struct fc_rport *rport = starget_to_rport(scsi_target(sc_cmd->device));
  1927. int rc = FAILED;
  1928. int rval;
  1929. rval = fc_block_scsi_eh(sc_cmd);
  1930. if (rval)
  1931. return rval;
  1932. lport = shost_priv(sc_cmd->device->host);
  1933. if (lport->state != LPORT_ST_READY)
  1934. return rc;
  1935. FC_SCSI_DBG(lport, "Resetting rport (%6.6x)\n", rport->port_id);
  1936. fsp = fc_fcp_pkt_alloc(lport, GFP_NOIO);
  1937. if (fsp == NULL) {
  1938. printk(KERN_WARNING "libfc: could not allocate scsi_pkt\n");
  1939. goto out;
  1940. }
  1941. /*
  1942. * Build the libfc request pkt. Do not set the scsi cmnd, because
  1943. * the sc passed in is not setup for execution like when sent
  1944. * through the queuecommand callout.
  1945. */
  1946. fsp->rport = rport; /* set the remote port ptr */
  1947. /*
  1948. * flush outstanding commands
  1949. */
  1950. rc = fc_lun_reset(lport, fsp, scmd_id(sc_cmd), sc_cmd->device->lun);
  1951. fsp->state = FC_SRB_FREE;
  1952. fc_fcp_pkt_release(fsp);
  1953. out:
  1954. return rc;
  1955. }
  1956. EXPORT_SYMBOL(fc_eh_device_reset);
  1957. /**
  1958. * fc_eh_host_reset() - Reset a Scsi_Host.
  1959. * @sc_cmd: The SCSI command that identifies the SCSI host to be reset
  1960. */
  1961. int fc_eh_host_reset(struct scsi_cmnd *sc_cmd)
  1962. {
  1963. struct Scsi_Host *shost = sc_cmd->device->host;
  1964. struct fc_lport *lport = shost_priv(shost);
  1965. unsigned long wait_tmo;
  1966. FC_SCSI_DBG(lport, "Resetting host\n");
  1967. fc_lport_reset(lport);
  1968. wait_tmo = jiffies + FC_HOST_RESET_TIMEOUT;
  1969. while (!fc_fcp_lport_queue_ready(lport) && time_before(jiffies,
  1970. wait_tmo))
  1971. msleep(1000);
  1972. if (fc_fcp_lport_queue_ready(lport)) {
  1973. shost_printk(KERN_INFO, shost, "libfc: Host reset succeeded "
  1974. "on port (%6.6x)\n", lport->port_id);
  1975. return SUCCESS;
  1976. } else {
  1977. shost_printk(KERN_INFO, shost, "libfc: Host reset failed, "
  1978. "port (%6.6x) is not ready.\n",
  1979. lport->port_id);
  1980. return FAILED;
  1981. }
  1982. }
  1983. EXPORT_SYMBOL(fc_eh_host_reset);
  1984. /**
  1985. * fc_slave_alloc() - Configure the queue depth of a Scsi_Host
  1986. * @sdev: The SCSI device that identifies the SCSI host
  1987. *
  1988. * Configures queue depth based on host's cmd_per_len. If not set
  1989. * then we use the libfc default.
  1990. */
  1991. int fc_slave_alloc(struct scsi_device *sdev)
  1992. {
  1993. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  1994. if (!rport || fc_remote_port_chkready(rport))
  1995. return -ENXIO;
  1996. scsi_change_queue_depth(sdev, FC_FCP_DFLT_QUEUE_DEPTH);
  1997. return 0;
  1998. }
  1999. EXPORT_SYMBOL(fc_slave_alloc);
  2000. /**
  2001. * fc_fcp_destroy() - Tear down the FCP layer for a given local port
  2002. * @lport: The local port that no longer needs the FCP layer
  2003. */
  2004. void fc_fcp_destroy(struct fc_lport *lport)
  2005. {
  2006. struct fc_fcp_internal *si = fc_get_scsi_internal(lport);
  2007. if (!list_empty(&si->scsi_pkt_queue))
  2008. printk(KERN_ERR "libfc: Leaked SCSI packets when destroying "
  2009. "port (%6.6x)\n", lport->port_id);
  2010. mempool_destroy(si->scsi_pkt_pool);
  2011. kfree(si);
  2012. lport->scsi_priv = NULL;
  2013. }
  2014. EXPORT_SYMBOL(fc_fcp_destroy);
  2015. int fc_setup_fcp(void)
  2016. {
  2017. int rc = 0;
  2018. scsi_pkt_cachep = kmem_cache_create("libfc_fcp_pkt",
  2019. sizeof(struct fc_fcp_pkt),
  2020. 0, SLAB_HWCACHE_ALIGN, NULL);
  2021. if (!scsi_pkt_cachep) {
  2022. printk(KERN_ERR "libfc: Unable to allocate SRB cache, "
  2023. "module load failed!");
  2024. rc = -ENOMEM;
  2025. }
  2026. return rc;
  2027. }
  2028. void fc_destroy_fcp(void)
  2029. {
  2030. kmem_cache_destroy(scsi_pkt_cachep);
  2031. }
  2032. /**
  2033. * fc_fcp_init() - Initialize the FCP layer for a local port
  2034. * @lport: The local port to initialize the exchange layer for
  2035. */
  2036. int fc_fcp_init(struct fc_lport *lport)
  2037. {
  2038. int rc;
  2039. struct fc_fcp_internal *si;
  2040. if (!lport->tt.fcp_cmd_send)
  2041. lport->tt.fcp_cmd_send = fc_fcp_cmd_send;
  2042. if (!lport->tt.fcp_cleanup)
  2043. lport->tt.fcp_cleanup = fc_fcp_cleanup;
  2044. if (!lport->tt.fcp_abort_io)
  2045. lport->tt.fcp_abort_io = fc_fcp_abort_io;
  2046. si = kzalloc(sizeof(struct fc_fcp_internal), GFP_KERNEL);
  2047. if (!si)
  2048. return -ENOMEM;
  2049. lport->scsi_priv = si;
  2050. si->max_can_queue = lport->host->can_queue;
  2051. INIT_LIST_HEAD(&si->scsi_pkt_queue);
  2052. spin_lock_init(&si->scsi_queue_lock);
  2053. si->scsi_pkt_pool = mempool_create_slab_pool(2, scsi_pkt_cachep);
  2054. if (!si->scsi_pkt_pool) {
  2055. rc = -ENOMEM;
  2056. goto free_internal;
  2057. }
  2058. return 0;
  2059. free_internal:
  2060. kfree(si);
  2061. return rc;
  2062. }
  2063. EXPORT_SYMBOL(fc_fcp_init);