libiscsi.c 51 KB

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