fc_fcp.c 54 KB

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