fc_fcp.c 60 KB

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