fc_fcp.c 58 KB

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