libiscsi.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995
  1. /*
  2. * iSCSI lib functions
  3. *
  4. * Copyright (C) 2006 Red Hat, Inc. All rights reserved.
  5. * Copyright (C) 2004 - 2006 Mike Christie
  6. * Copyright (C) 2004 - 2005 Dmitry Yusupov
  7. * Copyright (C) 2004 - 2005 Alex Aizman
  8. * maintained by open-iscsi@googlegroups.com
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23. */
  24. #include <linux/types.h>
  25. #include <linux/mutex.h>
  26. #include <linux/kfifo.h>
  27. #include <linux/delay.h>
  28. #include <net/tcp.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <scsi/scsi_device.h>
  31. #include <scsi/scsi_eh.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <scsi/scsi_host.h>
  34. #include <scsi/scsi.h>
  35. #include <scsi/iscsi_proto.h>
  36. #include <scsi/scsi_transport.h>
  37. #include <scsi/scsi_transport_iscsi.h>
  38. #include <scsi/libiscsi.h>
  39. struct iscsi_session *
  40. class_to_transport_session(struct iscsi_cls_session *cls_session)
  41. {
  42. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  43. return iscsi_hostdata(shost->hostdata);
  44. }
  45. EXPORT_SYMBOL_GPL(class_to_transport_session);
  46. #define INVALID_SN_DELTA 0xffff
  47. int
  48. iscsi_check_assign_cmdsn(struct iscsi_session *session, struct iscsi_nopin *hdr)
  49. {
  50. uint32_t max_cmdsn = be32_to_cpu(hdr->max_cmdsn);
  51. uint32_t exp_cmdsn = be32_to_cpu(hdr->exp_cmdsn);
  52. if (max_cmdsn < exp_cmdsn -1 &&
  53. max_cmdsn > exp_cmdsn - INVALID_SN_DELTA)
  54. return ISCSI_ERR_MAX_CMDSN;
  55. if (max_cmdsn > session->max_cmdsn ||
  56. max_cmdsn < session->max_cmdsn - INVALID_SN_DELTA)
  57. session->max_cmdsn = max_cmdsn;
  58. if (exp_cmdsn > session->exp_cmdsn ||
  59. exp_cmdsn < session->exp_cmdsn - INVALID_SN_DELTA)
  60. session->exp_cmdsn = exp_cmdsn;
  61. return 0;
  62. }
  63. EXPORT_SYMBOL_GPL(iscsi_check_assign_cmdsn);
  64. void iscsi_prep_unsolicit_data_pdu(struct iscsi_cmd_task *ctask,
  65. struct iscsi_data *hdr)
  66. {
  67. struct iscsi_conn *conn = ctask->conn;
  68. memset(hdr, 0, sizeof(struct iscsi_data));
  69. hdr->ttt = cpu_to_be32(ISCSI_RESERVED_TAG);
  70. hdr->datasn = cpu_to_be32(ctask->unsol_datasn);
  71. ctask->unsol_datasn++;
  72. hdr->opcode = ISCSI_OP_SCSI_DATA_OUT;
  73. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  74. hdr->itt = ctask->hdr->itt;
  75. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  76. hdr->offset = cpu_to_be32(ctask->unsol_offset);
  77. if (ctask->unsol_count > conn->max_xmit_dlength) {
  78. hton24(hdr->dlength, conn->max_xmit_dlength);
  79. ctask->data_count = conn->max_xmit_dlength;
  80. ctask->unsol_offset += ctask->data_count;
  81. hdr->flags = 0;
  82. } else {
  83. hton24(hdr->dlength, ctask->unsol_count);
  84. ctask->data_count = ctask->unsol_count;
  85. hdr->flags = ISCSI_FLAG_CMD_FINAL;
  86. }
  87. }
  88. EXPORT_SYMBOL_GPL(iscsi_prep_unsolicit_data_pdu);
  89. /**
  90. * iscsi_prep_scsi_cmd_pdu - prep iscsi scsi cmd pdu
  91. * @ctask: iscsi cmd task
  92. *
  93. * Prep basic iSCSI PDU fields for a scsi cmd pdu. The LLD should set
  94. * fields like dlength or final based on how much data it sends
  95. */
  96. static void iscsi_prep_scsi_cmd_pdu(struct iscsi_cmd_task *ctask)
  97. {
  98. struct iscsi_conn *conn = ctask->conn;
  99. struct iscsi_session *session = conn->session;
  100. struct iscsi_cmd *hdr = ctask->hdr;
  101. struct scsi_cmnd *sc = ctask->sc;
  102. hdr->opcode = ISCSI_OP_SCSI_CMD;
  103. hdr->flags = ISCSI_ATTR_SIMPLE;
  104. int_to_scsilun(sc->device->lun, (struct scsi_lun *)hdr->lun);
  105. hdr->itt = ctask->itt | (conn->id << ISCSI_CID_SHIFT) |
  106. (session->age << ISCSI_AGE_SHIFT);
  107. hdr->data_length = cpu_to_be32(sc->request_bufflen);
  108. hdr->cmdsn = cpu_to_be32(session->cmdsn);
  109. session->cmdsn++;
  110. hdr->exp_statsn = cpu_to_be32(conn->exp_statsn);
  111. memcpy(hdr->cdb, sc->cmnd, sc->cmd_len);
  112. memset(&hdr->cdb[sc->cmd_len], 0, MAX_COMMAND_SIZE - sc->cmd_len);
  113. ctask->data_count = 0;
  114. if (sc->sc_data_direction == DMA_TO_DEVICE) {
  115. hdr->flags |= ISCSI_FLAG_CMD_WRITE;
  116. /*
  117. * Write counters:
  118. *
  119. * imm_count bytes to be sent right after
  120. * SCSI PDU Header
  121. *
  122. * unsol_count bytes(as Data-Out) to be sent
  123. * without R2T ack right after
  124. * immediate data
  125. *
  126. * r2t_data_count bytes to be sent via R2T ack's
  127. *
  128. * pad_count bytes to be sent as zero-padding
  129. */
  130. ctask->imm_count = 0;
  131. ctask->unsol_count = 0;
  132. ctask->unsol_offset = 0;
  133. ctask->unsol_datasn = 0;
  134. if (session->imm_data_en) {
  135. if (ctask->total_length >= session->first_burst)
  136. ctask->imm_count = min(session->first_burst,
  137. conn->max_xmit_dlength);
  138. else
  139. ctask->imm_count = min(ctask->total_length,
  140. conn->max_xmit_dlength);
  141. hton24(ctask->hdr->dlength, ctask->imm_count);
  142. } else
  143. zero_data(ctask->hdr->dlength);
  144. if (!session->initial_r2t_en) {
  145. ctask->unsol_count = min(session->first_burst,
  146. ctask->total_length) - ctask->imm_count;
  147. ctask->unsol_offset = ctask->imm_count;
  148. }
  149. if (!ctask->unsol_count)
  150. /* No unsolicit Data-Out's */
  151. ctask->hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  152. } else {
  153. ctask->datasn = 0;
  154. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  155. zero_data(hdr->dlength);
  156. if (sc->sc_data_direction == DMA_FROM_DEVICE)
  157. hdr->flags |= ISCSI_FLAG_CMD_READ;
  158. }
  159. conn->scsicmd_pdus_cnt++;
  160. }
  161. EXPORT_SYMBOL_GPL(iscsi_prep_scsi_cmd_pdu);
  162. /**
  163. * iscsi_complete_command - return command back to scsi-ml
  164. * @ctask: iscsi cmd task
  165. *
  166. * Must be called with session lock.
  167. * This function returns the scsi command to scsi-ml and returns
  168. * the cmd task to the pool of available cmd tasks.
  169. */
  170. static void iscsi_complete_command(struct iscsi_cmd_task *ctask)
  171. {
  172. struct iscsi_session *session = ctask->conn->session;
  173. struct scsi_cmnd *sc = ctask->sc;
  174. ctask->state = ISCSI_TASK_COMPLETED;
  175. ctask->sc = NULL;
  176. /* SCSI eh reuses commands to verify us */
  177. sc->SCp.ptr = NULL;
  178. list_del_init(&ctask->running);
  179. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  180. sc->scsi_done(sc);
  181. }
  182. static void __iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  183. {
  184. atomic_inc(&ctask->refcount);
  185. }
  186. static void iscsi_get_ctask(struct iscsi_cmd_task *ctask)
  187. {
  188. spin_lock_bh(&ctask->conn->session->lock);
  189. __iscsi_get_ctask(ctask);
  190. spin_unlock_bh(&ctask->conn->session->lock);
  191. }
  192. static void __iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  193. {
  194. if (atomic_dec_and_test(&ctask->refcount))
  195. iscsi_complete_command(ctask);
  196. }
  197. static void iscsi_put_ctask(struct iscsi_cmd_task *ctask)
  198. {
  199. spin_lock_bh(&ctask->conn->session->lock);
  200. __iscsi_put_ctask(ctask);
  201. spin_unlock_bh(&ctask->conn->session->lock);
  202. }
  203. /**
  204. * iscsi_cmd_rsp - SCSI Command Response processing
  205. * @conn: iscsi connection
  206. * @hdr: iscsi header
  207. * @ctask: scsi command task
  208. * @data: cmd data buffer
  209. * @datalen: len of buffer
  210. *
  211. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  212. * then completes the command and task.
  213. **/
  214. static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  215. struct iscsi_cmd_task *ctask, char *data,
  216. int datalen)
  217. {
  218. int rc;
  219. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  220. struct iscsi_session *session = conn->session;
  221. struct scsi_cmnd *sc = ctask->sc;
  222. rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
  223. if (rc) {
  224. sc->result = DID_ERROR << 16;
  225. goto out;
  226. }
  227. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  228. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  229. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  230. sc->result = DID_ERROR << 16;
  231. goto out;
  232. }
  233. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  234. int senselen;
  235. if (datalen < 2) {
  236. invalid_datalen:
  237. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  238. "invalid data buffer size of %d\n", datalen);
  239. sc->result = DID_BAD_TARGET << 16;
  240. goto out;
  241. }
  242. senselen = (data[0] << 8) | data[1];
  243. if (datalen < senselen)
  244. goto invalid_datalen;
  245. memcpy(sc->sense_buffer, data + 2,
  246. min(senselen, SCSI_SENSE_BUFFERSIZE));
  247. debug_scsi("copied %d bytes of sense\n",
  248. min(senselen, SCSI_SENSE_BUFFERSIZE));
  249. }
  250. if (sc->sc_data_direction == DMA_TO_DEVICE)
  251. goto out;
  252. if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  253. int res_count = be32_to_cpu(rhdr->residual_count);
  254. if (res_count > 0 && res_count <= sc->request_bufflen)
  255. sc->resid = res_count;
  256. else
  257. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  258. } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
  259. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  260. else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
  261. sc->resid = be32_to_cpu(rhdr->residual_count);
  262. out:
  263. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  264. (long)sc, sc->result, ctask->itt);
  265. conn->scsirsp_pdus_cnt++;
  266. __iscsi_put_ctask(ctask);
  267. return rc;
  268. }
  269. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  270. {
  271. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  272. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  273. conn->tmfrsp_pdus_cnt++;
  274. if (conn->tmabort_state != TMABORT_INITIAL)
  275. return;
  276. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  277. conn->tmabort_state = TMABORT_SUCCESS;
  278. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  279. conn->tmabort_state = TMABORT_NOT_FOUND;
  280. else
  281. conn->tmabort_state = TMABORT_FAILED;
  282. wake_up(&conn->ehwait);
  283. }
  284. static int iscsi_handle_reject(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  285. char *data, int datalen)
  286. {
  287. struct iscsi_reject *reject = (struct iscsi_reject *)hdr;
  288. struct iscsi_hdr rejected_pdu;
  289. uint32_t itt;
  290. conn->exp_statsn = be32_to_cpu(reject->statsn) + 1;
  291. if (reject->reason == ISCSI_REASON_DATA_DIGEST_ERROR) {
  292. if (ntoh24(reject->dlength) > datalen)
  293. return ISCSI_ERR_PROTO;
  294. if (ntoh24(reject->dlength) >= sizeof(struct iscsi_hdr)) {
  295. memcpy(&rejected_pdu, data, sizeof(struct iscsi_hdr));
  296. itt = rejected_pdu.itt & ISCSI_ITT_MASK;
  297. printk(KERN_ERR "itt 0x%x had pdu (op 0x%x) rejected "
  298. "due to DataDigest error.\n", itt,
  299. rejected_pdu.opcode);
  300. }
  301. }
  302. return 0;
  303. }
  304. /**
  305. * __iscsi_complete_pdu - complete pdu
  306. * @conn: iscsi conn
  307. * @hdr: iscsi header
  308. * @data: data buffer
  309. * @datalen: len of data buffer
  310. *
  311. * Completes pdu processing by freeing any resources allocated at
  312. * queuecommand or send generic. session lock must be held and verify
  313. * itt must have been called.
  314. */
  315. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  316. char *data, int datalen)
  317. {
  318. struct iscsi_session *session = conn->session;
  319. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  320. struct iscsi_cmd_task *ctask;
  321. struct iscsi_mgmt_task *mtask;
  322. uint32_t itt;
  323. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
  324. itt = hdr->itt & ISCSI_ITT_MASK;
  325. else
  326. itt = hdr->itt;
  327. if (itt < session->cmds_max) {
  328. ctask = session->cmds[itt];
  329. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  330. opcode, conn->id, ctask->itt, datalen);
  331. switch(opcode) {
  332. case ISCSI_OP_SCSI_CMD_RSP:
  333. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  334. rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  335. datalen);
  336. break;
  337. case ISCSI_OP_SCSI_DATA_IN:
  338. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  339. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  340. conn->scsirsp_pdus_cnt++;
  341. __iscsi_put_ctask(ctask);
  342. }
  343. break;
  344. case ISCSI_OP_R2T:
  345. /* LLD handles this for now */
  346. break;
  347. default:
  348. rc = ISCSI_ERR_BAD_OPCODE;
  349. break;
  350. }
  351. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  352. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  353. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  354. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  355. opcode, conn->id, mtask->itt, datalen);
  356. rc = iscsi_check_assign_cmdsn(session,
  357. (struct iscsi_nopin*)hdr);
  358. if (rc)
  359. goto done;
  360. switch(opcode) {
  361. case ISCSI_OP_LOGOUT_RSP:
  362. if (datalen) {
  363. rc = ISCSI_ERR_PROTO;
  364. break;
  365. }
  366. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  367. /* fall through */
  368. case ISCSI_OP_LOGIN_RSP:
  369. case ISCSI_OP_TEXT_RSP:
  370. /*
  371. * login related PDU's exp_statsn is handled in
  372. * userspace
  373. */
  374. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  375. rc = ISCSI_ERR_CONN_FAILED;
  376. list_del(&mtask->running);
  377. if (conn->login_mtask != mtask)
  378. __kfifo_put(session->mgmtpool.queue,
  379. (void*)&mtask, sizeof(void*));
  380. break;
  381. case ISCSI_OP_SCSI_TMFUNC_RSP:
  382. if (datalen) {
  383. rc = ISCSI_ERR_PROTO;
  384. break;
  385. }
  386. iscsi_tmf_rsp(conn, hdr);
  387. break;
  388. case ISCSI_OP_NOOP_IN:
  389. if (hdr->ttt != ISCSI_RESERVED_TAG || datalen) {
  390. rc = ISCSI_ERR_PROTO;
  391. break;
  392. }
  393. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  394. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  395. rc = ISCSI_ERR_CONN_FAILED;
  396. list_del(&mtask->running);
  397. if (conn->login_mtask != mtask)
  398. __kfifo_put(session->mgmtpool.queue,
  399. (void*)&mtask, sizeof(void*));
  400. break;
  401. default:
  402. rc = ISCSI_ERR_BAD_OPCODE;
  403. break;
  404. }
  405. } else if (itt == ISCSI_RESERVED_TAG) {
  406. rc = iscsi_check_assign_cmdsn(session,
  407. (struct iscsi_nopin*)hdr);
  408. if (rc)
  409. goto done;
  410. switch(opcode) {
  411. case ISCSI_OP_NOOP_IN:
  412. if (datalen) {
  413. rc = ISCSI_ERR_PROTO;
  414. break;
  415. }
  416. if (hdr->ttt == ISCSI_RESERVED_TAG)
  417. break;
  418. if (iscsi_recv_pdu(conn->cls_conn, hdr, NULL, 0))
  419. rc = ISCSI_ERR_CONN_FAILED;
  420. break;
  421. case ISCSI_OP_REJECT:
  422. rc = iscsi_handle_reject(conn, hdr, data, datalen);
  423. break;
  424. case ISCSI_OP_ASYNC_EVENT:
  425. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  426. if (iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen))
  427. rc = ISCSI_ERR_CONN_FAILED;
  428. break;
  429. default:
  430. rc = ISCSI_ERR_BAD_OPCODE;
  431. break;
  432. }
  433. } else
  434. rc = ISCSI_ERR_BAD_ITT;
  435. done:
  436. return rc;
  437. }
  438. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  439. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  440. char *data, int datalen)
  441. {
  442. int rc;
  443. spin_lock(&conn->session->lock);
  444. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  445. spin_unlock(&conn->session->lock);
  446. return rc;
  447. }
  448. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  449. /* verify itt (itt encoding: age+cid+itt) */
  450. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  451. uint32_t *ret_itt)
  452. {
  453. struct iscsi_session *session = conn->session;
  454. struct iscsi_cmd_task *ctask;
  455. uint32_t itt;
  456. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  457. if ((hdr->itt & ISCSI_AGE_MASK) !=
  458. (session->age << ISCSI_AGE_SHIFT)) {
  459. printk(KERN_ERR "iscsi: received itt %x expected "
  460. "session age (%x)\n", hdr->itt,
  461. session->age & ISCSI_AGE_MASK);
  462. return ISCSI_ERR_BAD_ITT;
  463. }
  464. if ((hdr->itt & ISCSI_CID_MASK) !=
  465. (conn->id << ISCSI_CID_SHIFT)) {
  466. printk(KERN_ERR "iscsi: received itt %x, expected "
  467. "CID (%x)\n", hdr->itt, conn->id);
  468. return ISCSI_ERR_BAD_ITT;
  469. }
  470. itt = hdr->itt & ISCSI_ITT_MASK;
  471. } else
  472. itt = hdr->itt;
  473. if (itt < session->cmds_max) {
  474. ctask = session->cmds[itt];
  475. if (!ctask->sc) {
  476. printk(KERN_INFO "iscsi: dropping ctask with "
  477. "itt 0x%x\n", ctask->itt);
  478. /* force drop */
  479. return ISCSI_ERR_NO_SCSI_CMD;
  480. }
  481. if (ctask->sc->SCp.phase != session->age) {
  482. printk(KERN_ERR "iscsi: ctask's session age %d, "
  483. "expected %d\n", ctask->sc->SCp.phase,
  484. session->age);
  485. return ISCSI_ERR_SESSION_FAILED;
  486. }
  487. }
  488. *ret_itt = itt;
  489. return 0;
  490. }
  491. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  492. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  493. {
  494. struct iscsi_session *session = conn->session;
  495. unsigned long flags;
  496. spin_lock_irqsave(&session->lock, flags);
  497. if (session->state == ISCSI_STATE_FAILED) {
  498. spin_unlock_irqrestore(&session->lock, flags);
  499. return;
  500. }
  501. if (conn->stop_stage == 0)
  502. session->state = ISCSI_STATE_FAILED;
  503. spin_unlock_irqrestore(&session->lock, flags);
  504. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  505. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  506. iscsi_conn_error(conn->cls_conn, err);
  507. }
  508. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  509. static int iscsi_xmit_imm_task(struct iscsi_conn *conn)
  510. {
  511. struct iscsi_hdr *hdr = conn->mtask->hdr;
  512. int rc, was_logout = 0;
  513. if ((hdr->opcode & ISCSI_OPCODE_MASK) == ISCSI_OP_LOGOUT) {
  514. conn->session->state = ISCSI_STATE_IN_RECOVERY;
  515. iscsi_block_session(session_to_cls(conn->session));
  516. was_logout = 1;
  517. }
  518. rc = conn->session->tt->xmit_mgmt_task(conn, conn->mtask);
  519. if (rc)
  520. return rc;
  521. if (was_logout) {
  522. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  523. return -ENODATA;
  524. }
  525. return 0;
  526. }
  527. /**
  528. * iscsi_data_xmit - xmit any command into the scheduled connection
  529. * @conn: iscsi connection
  530. *
  531. * Notes:
  532. * The function can return -EAGAIN in which case the caller must
  533. * re-schedule it again later or recover. '0' return code means
  534. * successful xmit.
  535. **/
  536. static int iscsi_data_xmit(struct iscsi_conn *conn)
  537. {
  538. struct iscsi_transport *tt;
  539. int rc = 0;
  540. if (unlikely(conn->suspend_tx)) {
  541. debug_scsi("conn %d Tx suspended!\n", conn->id);
  542. return -ENODATA;
  543. }
  544. tt = conn->session->tt;
  545. /*
  546. * Transmit in the following order:
  547. *
  548. * 1) un-finished xmit (ctask or mtask)
  549. * 2) immediate control PDUs
  550. * 3) write data
  551. * 4) SCSI commands
  552. * 5) non-immediate control PDUs
  553. *
  554. * No need to lock around __kfifo_get as long as
  555. * there's one producer and one consumer.
  556. */
  557. BUG_ON(conn->ctask && conn->mtask);
  558. if (conn->ctask) {
  559. iscsi_get_ctask(conn->ctask);
  560. rc = tt->xmit_cmd_task(conn, conn->ctask);
  561. iscsi_put_ctask(conn->ctask);
  562. if (rc)
  563. goto again;
  564. /* done with this in-progress ctask */
  565. conn->ctask = NULL;
  566. }
  567. if (conn->mtask) {
  568. rc = iscsi_xmit_imm_task(conn);
  569. if (rc)
  570. goto again;
  571. /* done with this in-progress mtask */
  572. conn->mtask = NULL;
  573. }
  574. /* process immediate first */
  575. if (unlikely(__kfifo_len(conn->immqueue))) {
  576. while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
  577. sizeof(void*))) {
  578. spin_lock_bh(&conn->session->lock);
  579. list_add_tail(&conn->mtask->running,
  580. &conn->mgmt_run_list);
  581. spin_unlock_bh(&conn->session->lock);
  582. rc = iscsi_xmit_imm_task(conn);
  583. if (rc)
  584. goto again;
  585. }
  586. /* done with this mtask */
  587. conn->mtask = NULL;
  588. }
  589. /* process command queue */
  590. spin_lock_bh(&conn->session->lock);
  591. while (!list_empty(&conn->xmitqueue)) {
  592. /*
  593. * iscsi tcp may readd the task to the xmitqueue to send
  594. * write data
  595. */
  596. conn->ctask = list_entry(conn->xmitqueue.next,
  597. struct iscsi_cmd_task, running);
  598. conn->ctask->state = ISCSI_TASK_RUNNING;
  599. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  600. __iscsi_get_ctask(conn->ctask);
  601. spin_unlock_bh(&conn->session->lock);
  602. rc = tt->xmit_cmd_task(conn, conn->ctask);
  603. spin_lock_bh(&conn->session->lock);
  604. __iscsi_put_ctask(conn->ctask);
  605. if (rc) {
  606. spin_unlock_bh(&conn->session->lock);
  607. goto again;
  608. }
  609. }
  610. spin_unlock_bh(&conn->session->lock);
  611. /* done with this ctask */
  612. conn->ctask = NULL;
  613. /* process the rest control plane PDUs, if any */
  614. if (unlikely(__kfifo_len(conn->mgmtqueue))) {
  615. while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
  616. sizeof(void*))) {
  617. spin_lock_bh(&conn->session->lock);
  618. list_add_tail(&conn->mtask->running,
  619. &conn->mgmt_run_list);
  620. spin_unlock_bh(&conn->session->lock);
  621. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  622. if (rc)
  623. goto again;
  624. }
  625. /* done with this mtask */
  626. conn->mtask = NULL;
  627. }
  628. return -ENODATA;
  629. again:
  630. if (unlikely(conn->suspend_tx))
  631. return -ENODATA;
  632. return rc;
  633. }
  634. static void iscsi_xmitworker(void *data)
  635. {
  636. struct iscsi_conn *conn = data;
  637. int rc;
  638. /*
  639. * serialize Xmit worker on a per-connection basis.
  640. */
  641. mutex_lock(&conn->xmitmutex);
  642. do {
  643. rc = iscsi_data_xmit(conn);
  644. } while (rc >= 0 || rc == -EAGAIN);
  645. mutex_unlock(&conn->xmitmutex);
  646. }
  647. enum {
  648. FAILURE_BAD_HOST = 1,
  649. FAILURE_SESSION_FAILED,
  650. FAILURE_SESSION_FREED,
  651. FAILURE_WINDOW_CLOSED,
  652. FAILURE_OOM,
  653. FAILURE_SESSION_TERMINATE,
  654. FAILURE_SESSION_IN_RECOVERY,
  655. FAILURE_SESSION_RECOVERY_TIMEOUT,
  656. };
  657. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  658. {
  659. struct Scsi_Host *host;
  660. int reason = 0;
  661. struct iscsi_session *session;
  662. struct iscsi_conn *conn;
  663. struct iscsi_cmd_task *ctask = NULL;
  664. sc->scsi_done = done;
  665. sc->result = 0;
  666. sc->SCp.ptr = NULL;
  667. host = sc->device->host;
  668. session = iscsi_hostdata(host->hostdata);
  669. spin_lock(&session->lock);
  670. /*
  671. * ISCSI_STATE_FAILED is a temp. state. The recovery
  672. * code will decide what is best to do with command queued
  673. * during this time
  674. */
  675. if (session->state != ISCSI_STATE_LOGGED_IN &&
  676. session->state != ISCSI_STATE_FAILED) {
  677. /*
  678. * to handle the race between when we set the recovery state
  679. * and block the session we requeue here (commands could
  680. * be entering our queuecommand while a block is starting
  681. * up because the block code is not locked)
  682. */
  683. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  684. reason = FAILURE_SESSION_IN_RECOVERY;
  685. goto reject;
  686. }
  687. if (session->state == ISCSI_STATE_RECOVERY_FAILED)
  688. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  689. else if (session->state == ISCSI_STATE_TERMINATE)
  690. reason = FAILURE_SESSION_TERMINATE;
  691. else
  692. reason = FAILURE_SESSION_FREED;
  693. goto fault;
  694. }
  695. /*
  696. * Check for iSCSI window and take care of CmdSN wrap-around
  697. */
  698. if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
  699. reason = FAILURE_WINDOW_CLOSED;
  700. goto reject;
  701. }
  702. conn = session->leadconn;
  703. if (!conn) {
  704. reason = FAILURE_SESSION_FREED;
  705. goto fault;
  706. }
  707. if (!__kfifo_get(session->cmdpool.queue, (void*)&ctask,
  708. sizeof(void*))) {
  709. reason = FAILURE_OOM;
  710. goto reject;
  711. }
  712. sc->SCp.phase = session->age;
  713. sc->SCp.ptr = (char *)ctask;
  714. atomic_set(&ctask->refcount, 1);
  715. ctask->state = ISCSI_TASK_PENDING;
  716. ctask->mtask = NULL;
  717. ctask->conn = conn;
  718. ctask->sc = sc;
  719. INIT_LIST_HEAD(&ctask->running);
  720. ctask->total_length = sc->request_bufflen;
  721. iscsi_prep_scsi_cmd_pdu(ctask);
  722. session->tt->init_cmd_task(ctask);
  723. list_add_tail(&ctask->running, &conn->xmitqueue);
  724. debug_scsi(
  725. "ctask enq [%s cid %d sc %p cdb 0x%x itt 0x%x len %d cmdsn %d "
  726. "win %d]\n",
  727. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  728. conn->id, sc, sc->cmnd[0], ctask->itt, sc->request_bufflen,
  729. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  730. spin_unlock(&session->lock);
  731. scsi_queue_work(host, &conn->xmitwork);
  732. return 0;
  733. reject:
  734. spin_unlock(&session->lock);
  735. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  736. return SCSI_MLQUEUE_HOST_BUSY;
  737. fault:
  738. spin_unlock(&session->lock);
  739. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  740. sc->cmnd[0], reason);
  741. sc->result = (DID_NO_CONNECT << 16);
  742. sc->resid = sc->request_bufflen;
  743. sc->scsi_done(sc);
  744. return 0;
  745. }
  746. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  747. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  748. {
  749. if (depth > ISCSI_MAX_CMD_PER_LUN)
  750. depth = ISCSI_MAX_CMD_PER_LUN;
  751. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  752. return sdev->queue_depth;
  753. }
  754. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  755. static int
  756. iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  757. char *data, uint32_t data_size)
  758. {
  759. struct iscsi_session *session = conn->session;
  760. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  761. struct iscsi_mgmt_task *mtask;
  762. spin_lock_bh(&session->lock);
  763. if (session->state == ISCSI_STATE_TERMINATE) {
  764. spin_unlock_bh(&session->lock);
  765. return -EPERM;
  766. }
  767. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  768. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  769. /*
  770. * Login and Text are sent serially, in
  771. * request-followed-by-response sequence.
  772. * Same mtask can be used. Same ITT must be used.
  773. * Note that login_mtask is preallocated at conn_create().
  774. */
  775. mtask = conn->login_mtask;
  776. else {
  777. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  778. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  779. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  780. if (!__kfifo_get(session->mgmtpool.queue,
  781. (void*)&mtask, sizeof(void*))) {
  782. spin_unlock_bh(&session->lock);
  783. return -ENOSPC;
  784. }
  785. }
  786. /*
  787. * pre-format CmdSN for outgoing PDU.
  788. */
  789. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  790. hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
  791. (session->age << ISCSI_AGE_SHIFT);
  792. nop->cmdsn = cpu_to_be32(session->cmdsn);
  793. if (conn->c_stage == ISCSI_CONN_STARTED &&
  794. !(hdr->opcode & ISCSI_OP_IMMEDIATE))
  795. session->cmdsn++;
  796. } else
  797. /* do not advance CmdSN */
  798. nop->cmdsn = cpu_to_be32(session->cmdsn);
  799. if (data_size) {
  800. memcpy(mtask->data, data, data_size);
  801. mtask->data_count = data_size;
  802. } else
  803. mtask->data_count = 0;
  804. INIT_LIST_HEAD(&mtask->running);
  805. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  806. if (session->tt->init_mgmt_task)
  807. session->tt->init_mgmt_task(conn, mtask, data, data_size);
  808. spin_unlock_bh(&session->lock);
  809. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  810. hdr->opcode, hdr->itt, data_size);
  811. /*
  812. * since send_pdu() could be called at least from two contexts,
  813. * we need to serialize __kfifo_put, so we don't have to take
  814. * additional lock on fast data-path
  815. */
  816. if (hdr->opcode & ISCSI_OP_IMMEDIATE)
  817. __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
  818. else
  819. __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
  820. scsi_queue_work(session->host, &conn->xmitwork);
  821. return 0;
  822. }
  823. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  824. char *data, uint32_t data_size)
  825. {
  826. struct iscsi_conn *conn = cls_conn->dd_data;
  827. int rc;
  828. mutex_lock(&conn->xmitmutex);
  829. rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
  830. mutex_unlock(&conn->xmitmutex);
  831. return rc;
  832. }
  833. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  834. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  835. {
  836. struct iscsi_session *session = class_to_transport_session(cls_session);
  837. struct iscsi_conn *conn = session->leadconn;
  838. spin_lock_bh(&session->lock);
  839. if (session->state != ISCSI_STATE_LOGGED_IN) {
  840. session->state = ISCSI_STATE_RECOVERY_FAILED;
  841. if (conn)
  842. wake_up(&conn->ehwait);
  843. }
  844. spin_unlock_bh(&session->lock);
  845. }
  846. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  847. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  848. {
  849. struct Scsi_Host *host = sc->device->host;
  850. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  851. struct iscsi_conn *conn = session->leadconn;
  852. int fail_session = 0;
  853. spin_lock_bh(&session->lock);
  854. if (session->state == ISCSI_STATE_TERMINATE) {
  855. failed:
  856. debug_scsi("failing host reset: session terminated "
  857. "[CID %d age %d]\n", conn->id, session->age);
  858. spin_unlock_bh(&session->lock);
  859. return FAILED;
  860. }
  861. if (sc->SCp.phase == session->age) {
  862. debug_scsi("failing connection CID %d due to SCSI host reset\n",
  863. conn->id);
  864. fail_session = 1;
  865. }
  866. spin_unlock_bh(&session->lock);
  867. /*
  868. * we drop the lock here but the leadconn cannot be destoyed while
  869. * we are in the scsi eh
  870. */
  871. if (fail_session)
  872. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  873. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  874. wait_event_interruptible(conn->ehwait,
  875. session->state == ISCSI_STATE_TERMINATE ||
  876. session->state == ISCSI_STATE_LOGGED_IN ||
  877. session->state == ISCSI_STATE_RECOVERY_FAILED);
  878. if (signal_pending(current))
  879. flush_signals(current);
  880. spin_lock_bh(&session->lock);
  881. if (session->state == ISCSI_STATE_LOGGED_IN)
  882. printk(KERN_INFO "iscsi: host reset succeeded\n");
  883. else
  884. goto failed;
  885. spin_unlock_bh(&session->lock);
  886. return SUCCESS;
  887. }
  888. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  889. static void iscsi_tmabort_timedout(unsigned long data)
  890. {
  891. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
  892. struct iscsi_conn *conn = ctask->conn;
  893. struct iscsi_session *session = conn->session;
  894. spin_lock(&session->lock);
  895. if (conn->tmabort_state == TMABORT_INITIAL) {
  896. conn->tmabort_state = TMABORT_TIMEDOUT;
  897. debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
  898. ctask->sc, ctask->itt);
  899. /* unblock eh_abort() */
  900. wake_up(&conn->ehwait);
  901. }
  902. spin_unlock(&session->lock);
  903. }
  904. /* must be called with the mutex lock */
  905. static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
  906. struct iscsi_cmd_task *ctask)
  907. {
  908. struct iscsi_conn *conn = ctask->conn;
  909. struct iscsi_session *session = conn->session;
  910. struct iscsi_tm *hdr = &conn->tmhdr;
  911. int rc;
  912. /*
  913. * ctask timed out but session is OK requests must be serialized.
  914. */
  915. memset(hdr, 0, sizeof(struct iscsi_tm));
  916. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  917. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
  918. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  919. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  920. hdr->rtt = ctask->hdr->itt;
  921. hdr->refcmdsn = ctask->hdr->cmdsn;
  922. rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
  923. NULL, 0);
  924. if (rc) {
  925. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  926. debug_scsi("abort sent failure [itt 0x%x] %d\n", ctask->itt,
  927. rc);
  928. return rc;
  929. }
  930. debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
  931. spin_lock_bh(&session->lock);
  932. ctask->mtask = (struct iscsi_mgmt_task *)
  933. session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
  934. ISCSI_MGMT_ITT_OFFSET];
  935. if (conn->tmabort_state == TMABORT_INITIAL) {
  936. conn->tmfcmd_pdus_cnt++;
  937. conn->tmabort_timer.expires = 10*HZ + jiffies;
  938. conn->tmabort_timer.function = iscsi_tmabort_timedout;
  939. conn->tmabort_timer.data = (unsigned long)ctask;
  940. add_timer(&conn->tmabort_timer);
  941. debug_scsi("abort set timeout [itt 0x%x]\n", ctask->itt);
  942. }
  943. spin_unlock_bh(&session->lock);
  944. mutex_unlock(&conn->xmitmutex);
  945. /*
  946. * block eh thread until:
  947. *
  948. * 1) abort response
  949. * 2) abort timeout
  950. * 3) session is terminated or restarted or userspace has
  951. * given up on recovery
  952. */
  953. wait_event_interruptible(conn->ehwait,
  954. sc->SCp.phase != session->age ||
  955. session->state != ISCSI_STATE_LOGGED_IN ||
  956. conn->tmabort_state != TMABORT_INITIAL);
  957. if (signal_pending(current))
  958. flush_signals(current);
  959. del_timer_sync(&conn->tmabort_timer);
  960. mutex_lock(&conn->xmitmutex);
  961. return 0;
  962. }
  963. /*
  964. * xmit mutex and session lock must be held
  965. */
  966. static struct iscsi_mgmt_task *
  967. iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
  968. {
  969. int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
  970. struct iscsi_mgmt_task *task;
  971. debug_scsi("searching %d tasks\n", nr_tasks);
  972. for (i = 0; i < nr_tasks; i++) {
  973. __kfifo_get(fifo, (void*)&task, sizeof(void*));
  974. debug_scsi("check task %u\n", task->itt);
  975. if (task->itt == itt) {
  976. debug_scsi("matched task\n");
  977. return task;
  978. }
  979. __kfifo_put(fifo, (void*)&task, sizeof(void*));
  980. }
  981. return NULL;
  982. }
  983. static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
  984. {
  985. struct iscsi_conn *conn = ctask->conn;
  986. struct iscsi_session *session = conn->session;
  987. if (!ctask->mtask)
  988. return -EINVAL;
  989. if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
  990. list_del(&ctask->mtask->running);
  991. __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
  992. sizeof(void*));
  993. ctask->mtask = NULL;
  994. return 0;
  995. }
  996. /*
  997. * session lock and xmitmutex must be held
  998. */
  999. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  1000. int err)
  1001. {
  1002. struct scsi_cmnd *sc;
  1003. sc = ctask->sc;
  1004. if (!sc)
  1005. return;
  1006. conn->session->tt->cleanup_cmd_task(conn, ctask);
  1007. iscsi_ctask_mtask_cleanup(ctask);
  1008. sc->result = err;
  1009. sc->resid = sc->request_bufflen;
  1010. /* release ref from queuecommand */
  1011. __iscsi_put_ctask(ctask);
  1012. }
  1013. int iscsi_eh_abort(struct scsi_cmnd *sc)
  1014. {
  1015. struct iscsi_cmd_task *ctask;
  1016. struct iscsi_conn *conn;
  1017. struct iscsi_session *session;
  1018. int rc;
  1019. /*
  1020. * if session was ISCSI_STATE_IN_RECOVERY then we may not have
  1021. * got the command.
  1022. */
  1023. if (!sc->SCp.ptr) {
  1024. debug_scsi("sc never reached iscsi layer or it completed.\n");
  1025. return SUCCESS;
  1026. }
  1027. ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  1028. conn = ctask->conn;
  1029. session = conn->session;
  1030. conn->eh_abort_cnt++;
  1031. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  1032. mutex_lock(&conn->xmitmutex);
  1033. spin_lock_bh(&session->lock);
  1034. /*
  1035. * If we are not logged in or we have started a new session
  1036. * then let the host reset code handle this
  1037. */
  1038. if (session->state != ISCSI_STATE_LOGGED_IN ||
  1039. sc->SCp.phase != session->age)
  1040. goto failed;
  1041. /* ctask completed before time out */
  1042. if (!ctask->sc) {
  1043. spin_unlock_bh(&session->lock);
  1044. debug_scsi("sc completed while abort in progress\n");
  1045. goto success_rel_mutex;
  1046. }
  1047. /* what should we do here ? */
  1048. if (conn->ctask == ctask) {
  1049. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  1050. "Failing abort\n", sc, ctask->itt);
  1051. goto failed;
  1052. }
  1053. if (ctask->state == ISCSI_TASK_PENDING)
  1054. goto success_cleanup;
  1055. conn->tmabort_state = TMABORT_INITIAL;
  1056. spin_unlock_bh(&session->lock);
  1057. rc = iscsi_exec_abort_task(sc, ctask);
  1058. spin_lock_bh(&session->lock);
  1059. if (rc || sc->SCp.phase != session->age ||
  1060. session->state != ISCSI_STATE_LOGGED_IN)
  1061. goto failed;
  1062. iscsi_ctask_mtask_cleanup(ctask);
  1063. switch (conn->tmabort_state) {
  1064. case TMABORT_SUCCESS:
  1065. goto success_cleanup;
  1066. case TMABORT_NOT_FOUND:
  1067. if (!ctask->sc) {
  1068. /* ctask completed before tmf abort response */
  1069. spin_unlock_bh(&session->lock);
  1070. debug_scsi("sc completed while abort in progress\n");
  1071. goto success_rel_mutex;
  1072. }
  1073. /* fall through */
  1074. default:
  1075. /* timedout or failed */
  1076. spin_unlock_bh(&session->lock);
  1077. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  1078. spin_lock_bh(&session->lock);
  1079. goto failed;
  1080. }
  1081. success_cleanup:
  1082. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1083. spin_unlock_bh(&session->lock);
  1084. /*
  1085. * clean up task if aborted. we have the xmitmutex so grab
  1086. * the recv lock as a writer
  1087. */
  1088. write_lock_bh(conn->recv_lock);
  1089. spin_lock(&session->lock);
  1090. fail_command(conn, ctask, DID_ABORT << 16);
  1091. spin_unlock(&session->lock);
  1092. write_unlock_bh(conn->recv_lock);
  1093. success_rel_mutex:
  1094. mutex_unlock(&conn->xmitmutex);
  1095. return SUCCESS;
  1096. failed:
  1097. spin_unlock_bh(&session->lock);
  1098. mutex_unlock(&conn->xmitmutex);
  1099. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  1100. return FAILED;
  1101. }
  1102. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  1103. int
  1104. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1105. {
  1106. int i;
  1107. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1108. if (*items == NULL)
  1109. return -ENOMEM;
  1110. q->max = max;
  1111. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1112. if (q->pool == NULL) {
  1113. kfree(*items);
  1114. return -ENOMEM;
  1115. }
  1116. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1117. GFP_KERNEL, NULL);
  1118. if (q->queue == ERR_PTR(-ENOMEM)) {
  1119. kfree(q->pool);
  1120. kfree(*items);
  1121. return -ENOMEM;
  1122. }
  1123. for (i = 0; i < max; i++) {
  1124. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1125. if (q->pool[i] == NULL) {
  1126. int j;
  1127. for (j = 0; j < i; j++)
  1128. kfree(q->pool[j]);
  1129. kfifo_free(q->queue);
  1130. kfree(q->pool);
  1131. kfree(*items);
  1132. return -ENOMEM;
  1133. }
  1134. memset(q->pool[i], 0, item_size);
  1135. (*items)[i] = q->pool[i];
  1136. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1137. }
  1138. return 0;
  1139. }
  1140. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1141. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1142. {
  1143. int i;
  1144. for (i = 0; i < q->max; i++)
  1145. kfree(items[i]);
  1146. kfree(q->pool);
  1147. kfree(items);
  1148. }
  1149. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1150. /*
  1151. * iSCSI Session's hostdata organization:
  1152. *
  1153. * *------------------* <== hostdata_session(host->hostdata)
  1154. * | ptr to class sess|
  1155. * |------------------| <== iscsi_hostdata(host->hostdata)
  1156. * | iscsi_session |
  1157. * *------------------*
  1158. */
  1159. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1160. _sz % sizeof(unsigned long))
  1161. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1162. /**
  1163. * iscsi_session_setup - create iscsi cls session and host and session
  1164. * @scsit: scsi transport template
  1165. * @iscsit: iscsi transport template
  1166. * @initial_cmdsn: initial CmdSN
  1167. * @hostno: host no allocated
  1168. *
  1169. * This can be used by software iscsi_transports that allocate
  1170. * a session per scsi host.
  1171. **/
  1172. struct iscsi_cls_session *
  1173. iscsi_session_setup(struct iscsi_transport *iscsit,
  1174. struct scsi_transport_template *scsit,
  1175. int cmd_task_size, int mgmt_task_size,
  1176. uint32_t initial_cmdsn, uint32_t *hostno)
  1177. {
  1178. struct Scsi_Host *shost;
  1179. struct iscsi_session *session;
  1180. struct iscsi_cls_session *cls_session;
  1181. int cmd_i;
  1182. shost = scsi_host_alloc(iscsit->host_template,
  1183. hostdata_privsize(sizeof(*session)));
  1184. if (!shost)
  1185. return NULL;
  1186. shost->max_id = 1;
  1187. shost->max_channel = 0;
  1188. shost->max_lun = iscsit->max_lun;
  1189. shost->max_cmd_len = iscsit->max_cmd_len;
  1190. shost->transportt = scsit;
  1191. shost->transportt->create_work_queue = 1;
  1192. *hostno = shost->host_no;
  1193. session = iscsi_hostdata(shost->hostdata);
  1194. memset(session, 0, sizeof(struct iscsi_session));
  1195. session->host = shost;
  1196. session->state = ISCSI_STATE_FREE;
  1197. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1198. session->cmds_max = ISCSI_XMIT_CMDS_MAX;
  1199. session->cmdsn = initial_cmdsn;
  1200. session->exp_cmdsn = initial_cmdsn + 1;
  1201. session->max_cmdsn = initial_cmdsn + 1;
  1202. session->max_r2t = 1;
  1203. session->tt = iscsit;
  1204. /* initialize SCSI PDU commands pool */
  1205. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1206. (void***)&session->cmds,
  1207. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1208. goto cmdpool_alloc_fail;
  1209. /* pre-format cmds pool with ITT */
  1210. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1211. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1212. if (cmd_task_size)
  1213. ctask->dd_data = &ctask[1];
  1214. ctask->itt = cmd_i;
  1215. INIT_LIST_HEAD(&ctask->running);
  1216. }
  1217. spin_lock_init(&session->lock);
  1218. /* initialize immediate command pool */
  1219. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1220. (void***)&session->mgmt_cmds,
  1221. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1222. goto mgmtpool_alloc_fail;
  1223. /* pre-format immediate cmds pool with ITT */
  1224. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1225. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1226. if (mgmt_task_size)
  1227. mtask->dd_data = &mtask[1];
  1228. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1229. INIT_LIST_HEAD(&mtask->running);
  1230. }
  1231. if (scsi_add_host(shost, NULL))
  1232. goto add_host_fail;
  1233. if (!try_module_get(iscsit->owner))
  1234. goto cls_session_fail;
  1235. cls_session = iscsi_create_session(shost, iscsit, 0);
  1236. if (!cls_session)
  1237. goto module_put;
  1238. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1239. return cls_session;
  1240. module_put:
  1241. module_put(iscsit->owner);
  1242. cls_session_fail:
  1243. scsi_remove_host(shost);
  1244. add_host_fail:
  1245. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1246. mgmtpool_alloc_fail:
  1247. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1248. cmdpool_alloc_fail:
  1249. scsi_host_put(shost);
  1250. return NULL;
  1251. }
  1252. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1253. /**
  1254. * iscsi_session_teardown - destroy session, host, and cls_session
  1255. * shost: scsi host
  1256. *
  1257. * This can be used by software iscsi_transports that allocate
  1258. * a session per scsi host.
  1259. **/
  1260. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1261. {
  1262. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1263. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1264. struct module *owner = cls_session->transport->owner;
  1265. scsi_remove_host(shost);
  1266. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1267. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1268. kfree(session->targetname);
  1269. iscsi_destroy_session(cls_session);
  1270. scsi_host_put(shost);
  1271. module_put(owner);
  1272. }
  1273. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1274. /**
  1275. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1276. * @cls_session: iscsi_cls_session
  1277. * @conn_idx: cid
  1278. **/
  1279. struct iscsi_cls_conn *
  1280. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1281. {
  1282. struct iscsi_session *session = class_to_transport_session(cls_session);
  1283. struct iscsi_conn *conn;
  1284. struct iscsi_cls_conn *cls_conn;
  1285. char *data;
  1286. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1287. if (!cls_conn)
  1288. return NULL;
  1289. conn = cls_conn->dd_data;
  1290. memset(conn, 0, sizeof(*conn));
  1291. conn->session = session;
  1292. conn->cls_conn = cls_conn;
  1293. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1294. conn->id = conn_idx;
  1295. conn->exp_statsn = 0;
  1296. conn->tmabort_state = TMABORT_INITIAL;
  1297. INIT_LIST_HEAD(&conn->run_list);
  1298. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1299. INIT_LIST_HEAD(&conn->xmitqueue);
  1300. /* initialize general immediate & non-immediate PDU commands queue */
  1301. conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1302. GFP_KERNEL, NULL);
  1303. if (conn->immqueue == ERR_PTR(-ENOMEM))
  1304. goto immqueue_alloc_fail;
  1305. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1306. GFP_KERNEL, NULL);
  1307. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1308. goto mgmtqueue_alloc_fail;
  1309. INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
  1310. /* allocate login_mtask used for the login/text sequences */
  1311. spin_lock_bh(&session->lock);
  1312. if (!__kfifo_get(session->mgmtpool.queue,
  1313. (void*)&conn->login_mtask,
  1314. sizeof(void*))) {
  1315. spin_unlock_bh(&session->lock);
  1316. goto login_mtask_alloc_fail;
  1317. }
  1318. spin_unlock_bh(&session->lock);
  1319. data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
  1320. if (!data)
  1321. goto login_mtask_data_alloc_fail;
  1322. conn->login_mtask->data = conn->data = data;
  1323. init_timer(&conn->tmabort_timer);
  1324. mutex_init(&conn->xmitmutex);
  1325. init_waitqueue_head(&conn->ehwait);
  1326. return cls_conn;
  1327. login_mtask_data_alloc_fail:
  1328. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1329. sizeof(void*));
  1330. login_mtask_alloc_fail:
  1331. kfifo_free(conn->mgmtqueue);
  1332. mgmtqueue_alloc_fail:
  1333. kfifo_free(conn->immqueue);
  1334. immqueue_alloc_fail:
  1335. iscsi_destroy_conn(cls_conn);
  1336. return NULL;
  1337. }
  1338. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1339. /**
  1340. * iscsi_conn_teardown - teardown iscsi connection
  1341. * cls_conn: iscsi class connection
  1342. *
  1343. * TODO: we may need to make this into a two step process
  1344. * like scsi-mls remove + put host
  1345. */
  1346. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1347. {
  1348. struct iscsi_conn *conn = cls_conn->dd_data;
  1349. struct iscsi_session *session = conn->session;
  1350. unsigned long flags;
  1351. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1352. mutex_lock(&conn->xmitmutex);
  1353. spin_lock_bh(&session->lock);
  1354. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1355. if (session->leadconn == conn) {
  1356. /*
  1357. * leading connection? then give up on recovery.
  1358. */
  1359. session->state = ISCSI_STATE_TERMINATE;
  1360. wake_up(&conn->ehwait);
  1361. }
  1362. spin_unlock_bh(&session->lock);
  1363. mutex_unlock(&conn->xmitmutex);
  1364. /*
  1365. * Block until all in-progress commands for this connection
  1366. * time out or fail.
  1367. */
  1368. for (;;) {
  1369. spin_lock_irqsave(session->host->host_lock, flags);
  1370. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1371. spin_unlock_irqrestore(session->host->host_lock, flags);
  1372. break;
  1373. }
  1374. spin_unlock_irqrestore(session->host->host_lock, flags);
  1375. msleep_interruptible(500);
  1376. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1377. "host_failed %d\n", session->host->host_busy,
  1378. session->host->host_failed);
  1379. /*
  1380. * force eh_abort() to unblock
  1381. */
  1382. wake_up(&conn->ehwait);
  1383. }
  1384. spin_lock_bh(&session->lock);
  1385. kfree(conn->data);
  1386. kfree(conn->persistent_address);
  1387. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1388. sizeof(void*));
  1389. if (session->leadconn == conn) {
  1390. session->leadconn = NULL;
  1391. /* no connections exits.. reset sequencing */
  1392. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1393. }
  1394. spin_unlock_bh(&session->lock);
  1395. kfifo_free(conn->immqueue);
  1396. kfifo_free(conn->mgmtqueue);
  1397. iscsi_destroy_conn(cls_conn);
  1398. }
  1399. EXPORT_SYMBOL_GPL(iscsi_conn_teardown);
  1400. int iscsi_conn_start(struct iscsi_cls_conn *cls_conn)
  1401. {
  1402. struct iscsi_conn *conn = cls_conn->dd_data;
  1403. struct iscsi_session *session = conn->session;
  1404. if (!session) {
  1405. printk(KERN_ERR "iscsi: can't start unbound connection\n");
  1406. return -EPERM;
  1407. }
  1408. if ((session->imm_data_en || !session->initial_r2t_en) &&
  1409. session->first_burst > session->max_burst) {
  1410. printk("iscsi: invalid burst lengths: "
  1411. "first_burst %d max_burst %d\n",
  1412. session->first_burst, session->max_burst);
  1413. return -EINVAL;
  1414. }
  1415. spin_lock_bh(&session->lock);
  1416. conn->c_stage = ISCSI_CONN_STARTED;
  1417. session->state = ISCSI_STATE_LOGGED_IN;
  1418. switch(conn->stop_stage) {
  1419. case STOP_CONN_RECOVER:
  1420. /*
  1421. * unblock eh_abort() if it is blocked. re-try all
  1422. * commands after successful recovery
  1423. */
  1424. conn->stop_stage = 0;
  1425. conn->tmabort_state = TMABORT_INITIAL;
  1426. session->age++;
  1427. spin_unlock_bh(&session->lock);
  1428. iscsi_unblock_session(session_to_cls(session));
  1429. wake_up(&conn->ehwait);
  1430. return 0;
  1431. case STOP_CONN_TERM:
  1432. conn->stop_stage = 0;
  1433. break;
  1434. default:
  1435. break;
  1436. }
  1437. spin_unlock_bh(&session->lock);
  1438. return 0;
  1439. }
  1440. EXPORT_SYMBOL_GPL(iscsi_conn_start);
  1441. static void
  1442. flush_control_queues(struct iscsi_session *session, struct iscsi_conn *conn)
  1443. {
  1444. struct iscsi_mgmt_task *mtask, *tmp;
  1445. /* handle pending */
  1446. while (__kfifo_get(conn->immqueue, (void*)&mtask, sizeof(void*)) ||
  1447. __kfifo_get(conn->mgmtqueue, (void*)&mtask, sizeof(void*))) {
  1448. if (mtask == conn->login_mtask)
  1449. continue;
  1450. debug_scsi("flushing pending mgmt task itt 0x%x\n", mtask->itt);
  1451. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1452. sizeof(void*));
  1453. }
  1454. /* handle running */
  1455. list_for_each_entry_safe(mtask, tmp, &conn->mgmt_run_list, running) {
  1456. debug_scsi("flushing running mgmt task itt 0x%x\n", mtask->itt);
  1457. list_del(&mtask->running);
  1458. if (mtask == conn->login_mtask)
  1459. continue;
  1460. __kfifo_put(session->mgmtpool.queue, (void*)&mtask,
  1461. sizeof(void*));
  1462. }
  1463. conn->mtask = NULL;
  1464. }
  1465. /* Fail commands. Mutex and session lock held and recv side suspended */
  1466. static void fail_all_commands(struct iscsi_conn *conn)
  1467. {
  1468. struct iscsi_cmd_task *ctask, *tmp;
  1469. /* flush pending */
  1470. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  1471. debug_scsi("failing pending sc %p itt 0x%x\n", ctask->sc,
  1472. ctask->itt);
  1473. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1474. }
  1475. /* fail all other running */
  1476. list_for_each_entry_safe(ctask, tmp, &conn->run_list, running) {
  1477. debug_scsi("failing in progress sc %p itt 0x%x\n",
  1478. ctask->sc, ctask->itt);
  1479. fail_command(conn, ctask, DID_BUS_BUSY << 16);
  1480. }
  1481. conn->ctask = NULL;
  1482. }
  1483. static void iscsi_start_session_recovery(struct iscsi_session *session,
  1484. struct iscsi_conn *conn, int flag)
  1485. {
  1486. int old_stop_stage;
  1487. spin_lock_bh(&session->lock);
  1488. if (conn->stop_stage == STOP_CONN_TERM) {
  1489. spin_unlock_bh(&session->lock);
  1490. return;
  1491. }
  1492. /*
  1493. * When this is called for the in_login state, we only want to clean
  1494. * up the login task and connection. We do not need to block and set
  1495. * the recovery state again
  1496. */
  1497. if (flag == STOP_CONN_TERM)
  1498. session->state = ISCSI_STATE_TERMINATE;
  1499. else if (conn->stop_stage != STOP_CONN_RECOVER)
  1500. session->state = ISCSI_STATE_IN_RECOVERY;
  1501. old_stop_stage = conn->stop_stage;
  1502. conn->stop_stage = flag;
  1503. conn->c_stage = ISCSI_CONN_STOPPED;
  1504. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1505. spin_unlock_bh(&session->lock);
  1506. write_lock_bh(conn->recv_lock);
  1507. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1508. write_unlock_bh(conn->recv_lock);
  1509. mutex_lock(&conn->xmitmutex);
  1510. /*
  1511. * for connection level recovery we should not calculate
  1512. * header digest. conn->hdr_size used for optimization
  1513. * in hdr_extract() and will be re-negotiated at
  1514. * set_param() time.
  1515. */
  1516. if (flag == STOP_CONN_RECOVER) {
  1517. conn->hdrdgst_en = 0;
  1518. conn->datadgst_en = 0;
  1519. if (session->state == ISCSI_STATE_IN_RECOVERY &&
  1520. old_stop_stage != STOP_CONN_RECOVER) {
  1521. debug_scsi("blocking session\n");
  1522. iscsi_block_session(session_to_cls(session));
  1523. }
  1524. }
  1525. /*
  1526. * flush queues.
  1527. */
  1528. spin_lock_bh(&session->lock);
  1529. fail_all_commands(conn);
  1530. flush_control_queues(session, conn);
  1531. spin_unlock_bh(&session->lock);
  1532. mutex_unlock(&conn->xmitmutex);
  1533. }
  1534. void iscsi_conn_stop(struct iscsi_cls_conn *cls_conn, int flag)
  1535. {
  1536. struct iscsi_conn *conn = cls_conn->dd_data;
  1537. struct iscsi_session *session = conn->session;
  1538. switch (flag) {
  1539. case STOP_CONN_RECOVER:
  1540. case STOP_CONN_TERM:
  1541. iscsi_start_session_recovery(session, conn, flag);
  1542. break;
  1543. default:
  1544. printk(KERN_ERR "iscsi: invalid stop flag %d\n", flag);
  1545. }
  1546. }
  1547. EXPORT_SYMBOL_GPL(iscsi_conn_stop);
  1548. int iscsi_conn_bind(struct iscsi_cls_session *cls_session,
  1549. struct iscsi_cls_conn *cls_conn, int is_leading)
  1550. {
  1551. struct iscsi_session *session = class_to_transport_session(cls_session);
  1552. struct iscsi_conn *conn = cls_conn->dd_data;
  1553. spin_lock_bh(&session->lock);
  1554. if (is_leading)
  1555. session->leadconn = conn;
  1556. spin_unlock_bh(&session->lock);
  1557. /*
  1558. * Unblock xmitworker(), Login Phase will pass through.
  1559. */
  1560. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  1561. clear_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1562. return 0;
  1563. }
  1564. EXPORT_SYMBOL_GPL(iscsi_conn_bind);
  1565. int iscsi_set_param(struct iscsi_cls_conn *cls_conn,
  1566. enum iscsi_param param, char *buf, int buflen)
  1567. {
  1568. struct iscsi_conn *conn = cls_conn->dd_data;
  1569. struct iscsi_session *session = conn->session;
  1570. uint32_t value;
  1571. switch(param) {
  1572. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1573. sscanf(buf, "%d", &conn->max_recv_dlength);
  1574. break;
  1575. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1576. sscanf(buf, "%d", &conn->max_xmit_dlength);
  1577. break;
  1578. case ISCSI_PARAM_HDRDGST_EN:
  1579. sscanf(buf, "%d", &conn->hdrdgst_en);
  1580. break;
  1581. case ISCSI_PARAM_DATADGST_EN:
  1582. sscanf(buf, "%d", &conn->datadgst_en);
  1583. break;
  1584. case ISCSI_PARAM_INITIAL_R2T_EN:
  1585. sscanf(buf, "%d", &session->initial_r2t_en);
  1586. break;
  1587. case ISCSI_PARAM_MAX_R2T:
  1588. sscanf(buf, "%d", &session->max_r2t);
  1589. break;
  1590. case ISCSI_PARAM_IMM_DATA_EN:
  1591. sscanf(buf, "%d", &session->imm_data_en);
  1592. break;
  1593. case ISCSI_PARAM_FIRST_BURST:
  1594. sscanf(buf, "%d", &session->first_burst);
  1595. break;
  1596. case ISCSI_PARAM_MAX_BURST:
  1597. sscanf(buf, "%d", &session->max_burst);
  1598. break;
  1599. case ISCSI_PARAM_PDU_INORDER_EN:
  1600. sscanf(buf, "%d", &session->pdu_inorder_en);
  1601. break;
  1602. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1603. sscanf(buf, "%d", &session->dataseq_inorder_en);
  1604. break;
  1605. case ISCSI_PARAM_ERL:
  1606. sscanf(buf, "%d", &session->erl);
  1607. break;
  1608. case ISCSI_PARAM_IFMARKER_EN:
  1609. sscanf(buf, "%d", &value);
  1610. BUG_ON(value);
  1611. break;
  1612. case ISCSI_PARAM_OFMARKER_EN:
  1613. sscanf(buf, "%d", &value);
  1614. BUG_ON(value);
  1615. break;
  1616. case ISCSI_PARAM_EXP_STATSN:
  1617. sscanf(buf, "%u", &conn->exp_statsn);
  1618. break;
  1619. case ISCSI_PARAM_TARGET_NAME:
  1620. /* this should not change between logins */
  1621. if (session->targetname)
  1622. break;
  1623. session->targetname = kstrdup(buf, GFP_KERNEL);
  1624. if (!session->targetname)
  1625. return -ENOMEM;
  1626. break;
  1627. case ISCSI_PARAM_TPGT:
  1628. sscanf(buf, "%d", &session->tpgt);
  1629. break;
  1630. case ISCSI_PARAM_PERSISTENT_PORT:
  1631. sscanf(buf, "%d", &conn->persistent_port);
  1632. break;
  1633. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1634. /*
  1635. * this is the address returned in discovery so it should
  1636. * not change between logins.
  1637. */
  1638. if (conn->persistent_address)
  1639. break;
  1640. conn->persistent_address = kstrdup(buf, GFP_KERNEL);
  1641. if (!conn->persistent_address)
  1642. return -ENOMEM;
  1643. break;
  1644. default:
  1645. return -ENOSYS;
  1646. }
  1647. return 0;
  1648. }
  1649. EXPORT_SYMBOL_GPL(iscsi_set_param);
  1650. int iscsi_session_get_param(struct iscsi_cls_session *cls_session,
  1651. enum iscsi_param param, char *buf)
  1652. {
  1653. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1654. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1655. int len;
  1656. switch(param) {
  1657. case ISCSI_PARAM_INITIAL_R2T_EN:
  1658. len = sprintf(buf, "%d\n", session->initial_r2t_en);
  1659. break;
  1660. case ISCSI_PARAM_MAX_R2T:
  1661. len = sprintf(buf, "%hu\n", session->max_r2t);
  1662. break;
  1663. case ISCSI_PARAM_IMM_DATA_EN:
  1664. len = sprintf(buf, "%d\n", session->imm_data_en);
  1665. break;
  1666. case ISCSI_PARAM_FIRST_BURST:
  1667. len = sprintf(buf, "%u\n", session->first_burst);
  1668. break;
  1669. case ISCSI_PARAM_MAX_BURST:
  1670. len = sprintf(buf, "%u\n", session->max_burst);
  1671. break;
  1672. case ISCSI_PARAM_PDU_INORDER_EN:
  1673. len = sprintf(buf, "%d\n", session->pdu_inorder_en);
  1674. break;
  1675. case ISCSI_PARAM_DATASEQ_INORDER_EN:
  1676. len = sprintf(buf, "%d\n", session->dataseq_inorder_en);
  1677. break;
  1678. case ISCSI_PARAM_ERL:
  1679. len = sprintf(buf, "%d\n", session->erl);
  1680. break;
  1681. case ISCSI_PARAM_TARGET_NAME:
  1682. len = sprintf(buf, "%s\n", session->targetname);
  1683. break;
  1684. case ISCSI_PARAM_TPGT:
  1685. len = sprintf(buf, "%d\n", session->tpgt);
  1686. break;
  1687. default:
  1688. return -ENOSYS;
  1689. }
  1690. return len;
  1691. }
  1692. EXPORT_SYMBOL_GPL(iscsi_session_get_param);
  1693. int iscsi_conn_get_param(struct iscsi_cls_conn *cls_conn,
  1694. enum iscsi_param param, char *buf)
  1695. {
  1696. struct iscsi_conn *conn = cls_conn->dd_data;
  1697. int len;
  1698. switch(param) {
  1699. case ISCSI_PARAM_MAX_RECV_DLENGTH:
  1700. len = sprintf(buf, "%u\n", conn->max_recv_dlength);
  1701. break;
  1702. case ISCSI_PARAM_MAX_XMIT_DLENGTH:
  1703. len = sprintf(buf, "%u\n", conn->max_xmit_dlength);
  1704. break;
  1705. case ISCSI_PARAM_HDRDGST_EN:
  1706. len = sprintf(buf, "%d\n", conn->hdrdgst_en);
  1707. break;
  1708. case ISCSI_PARAM_DATADGST_EN:
  1709. len = sprintf(buf, "%d\n", conn->datadgst_en);
  1710. break;
  1711. case ISCSI_PARAM_IFMARKER_EN:
  1712. len = sprintf(buf, "%d\n", conn->ifmarker_en);
  1713. break;
  1714. case ISCSI_PARAM_OFMARKER_EN:
  1715. len = sprintf(buf, "%d\n", conn->ofmarker_en);
  1716. break;
  1717. case ISCSI_PARAM_EXP_STATSN:
  1718. len = sprintf(buf, "%u\n", conn->exp_statsn);
  1719. break;
  1720. case ISCSI_PARAM_PERSISTENT_PORT:
  1721. len = sprintf(buf, "%d\n", conn->persistent_port);
  1722. break;
  1723. case ISCSI_PARAM_PERSISTENT_ADDRESS:
  1724. len = sprintf(buf, "%s\n", conn->persistent_address);
  1725. break;
  1726. default:
  1727. return -ENOSYS;
  1728. }
  1729. return len;
  1730. }
  1731. EXPORT_SYMBOL_GPL(iscsi_conn_get_param);
  1732. MODULE_AUTHOR("Mike Christie");
  1733. MODULE_DESCRIPTION("iSCSI library functions");
  1734. MODULE_LICENSE("GPL");