libiscsi.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  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->state = ISCSI_TASK_COMPLETED;
  174. ctask->sc = NULL;
  175. list_del_init(&ctask->running);
  176. __kfifo_put(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  177. sc->scsi_done(sc);
  178. }
  179. /**
  180. * iscsi_cmd_rsp - SCSI Command Response processing
  181. * @conn: iscsi connection
  182. * @hdr: iscsi header
  183. * @ctask: scsi command task
  184. * @data: cmd data buffer
  185. * @datalen: len of buffer
  186. *
  187. * iscsi_cmd_rsp sets up the scsi_cmnd fields based on the PDU and
  188. * then completes the command and task.
  189. **/
  190. static int iscsi_scsi_cmd_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  191. struct iscsi_cmd_task *ctask, char *data,
  192. int datalen)
  193. {
  194. int rc;
  195. struct iscsi_cmd_rsp *rhdr = (struct iscsi_cmd_rsp *)hdr;
  196. struct iscsi_session *session = conn->session;
  197. struct scsi_cmnd *sc = ctask->sc;
  198. rc = iscsi_check_assign_cmdsn(session, (struct iscsi_nopin*)rhdr);
  199. if (rc) {
  200. sc->result = DID_ERROR << 16;
  201. goto out;
  202. }
  203. conn->exp_statsn = be32_to_cpu(rhdr->statsn) + 1;
  204. sc->result = (DID_OK << 16) | rhdr->cmd_status;
  205. if (rhdr->response != ISCSI_STATUS_CMD_COMPLETED) {
  206. sc->result = DID_ERROR << 16;
  207. goto out;
  208. }
  209. if (rhdr->cmd_status == SAM_STAT_CHECK_CONDITION) {
  210. int senselen;
  211. if (datalen < 2) {
  212. invalid_datalen:
  213. printk(KERN_ERR "iscsi: Got CHECK_CONDITION but "
  214. "invalid data buffer size of %d\n", datalen);
  215. sc->result = DID_BAD_TARGET << 16;
  216. goto out;
  217. }
  218. senselen = (data[0] << 8) | data[1];
  219. if (datalen < senselen)
  220. goto invalid_datalen;
  221. memcpy(sc->sense_buffer, data + 2,
  222. min(senselen, SCSI_SENSE_BUFFERSIZE));
  223. debug_scsi("copied %d bytes of sense\n",
  224. min(senselen, SCSI_SENSE_BUFFERSIZE));
  225. }
  226. if (sc->sc_data_direction == DMA_TO_DEVICE)
  227. goto out;
  228. if (rhdr->flags & ISCSI_FLAG_CMD_UNDERFLOW) {
  229. int res_count = be32_to_cpu(rhdr->residual_count);
  230. if (res_count > 0 && res_count <= sc->request_bufflen)
  231. sc->resid = res_count;
  232. else
  233. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  234. } else if (rhdr->flags & ISCSI_FLAG_CMD_BIDI_UNDERFLOW)
  235. sc->result = (DID_BAD_TARGET << 16) | rhdr->cmd_status;
  236. else if (rhdr->flags & ISCSI_FLAG_CMD_OVERFLOW)
  237. sc->resid = be32_to_cpu(rhdr->residual_count);
  238. out:
  239. debug_scsi("done [sc %lx res %d itt 0x%x]\n",
  240. (long)sc, sc->result, ctask->itt);
  241. conn->scsirsp_pdus_cnt++;
  242. iscsi_complete_command(conn->session, ctask);
  243. return rc;
  244. }
  245. static void iscsi_tmf_rsp(struct iscsi_conn *conn, struct iscsi_hdr *hdr)
  246. {
  247. struct iscsi_tm_rsp *tmf = (struct iscsi_tm_rsp *)hdr;
  248. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  249. conn->tmfrsp_pdus_cnt++;
  250. if (conn->tmabort_state != TMABORT_INITIAL)
  251. return;
  252. if (tmf->response == ISCSI_TMF_RSP_COMPLETE)
  253. conn->tmabort_state = TMABORT_SUCCESS;
  254. else if (tmf->response == ISCSI_TMF_RSP_NO_TASK)
  255. conn->tmabort_state = TMABORT_NOT_FOUND;
  256. else
  257. conn->tmabort_state = TMABORT_FAILED;
  258. wake_up(&conn->ehwait);
  259. }
  260. /**
  261. * __iscsi_complete_pdu - complete pdu
  262. * @conn: iscsi conn
  263. * @hdr: iscsi header
  264. * @data: data buffer
  265. * @datalen: len of data buffer
  266. *
  267. * Completes pdu processing by freeing any resources allocated at
  268. * queuecommand or send generic. session lock must be held and verify
  269. * itt must have been called.
  270. */
  271. int __iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  272. char *data, int datalen)
  273. {
  274. struct iscsi_session *session = conn->session;
  275. int opcode = hdr->opcode & ISCSI_OPCODE_MASK, rc = 0;
  276. struct iscsi_cmd_task *ctask;
  277. struct iscsi_mgmt_task *mtask;
  278. uint32_t itt;
  279. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG))
  280. itt = hdr->itt & ISCSI_ITT_MASK;
  281. else
  282. itt = hdr->itt;
  283. if (itt < session->cmds_max) {
  284. ctask = session->cmds[itt];
  285. debug_scsi("cmdrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  286. opcode, conn->id, ctask->itt, datalen);
  287. switch(opcode) {
  288. case ISCSI_OP_SCSI_CMD_RSP:
  289. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  290. rc = iscsi_scsi_cmd_rsp(conn, hdr, ctask, data,
  291. datalen);
  292. break;
  293. case ISCSI_OP_SCSI_DATA_IN:
  294. BUG_ON((void*)ctask != ctask->sc->SCp.ptr);
  295. if (hdr->flags & ISCSI_FLAG_DATA_STATUS) {
  296. conn->scsirsp_pdus_cnt++;
  297. iscsi_complete_command(session, ctask);
  298. }
  299. break;
  300. case ISCSI_OP_R2T:
  301. /* LLD handles this for now */
  302. break;
  303. default:
  304. rc = ISCSI_ERR_BAD_OPCODE;
  305. break;
  306. }
  307. } else if (itt >= ISCSI_MGMT_ITT_OFFSET &&
  308. itt < ISCSI_MGMT_ITT_OFFSET + session->mgmtpool_max) {
  309. mtask = session->mgmt_cmds[itt - ISCSI_MGMT_ITT_OFFSET];
  310. debug_scsi("immrsp [op 0x%x cid %d itt 0x%x len %d]\n",
  311. opcode, conn->id, mtask->itt, datalen);
  312. rc = iscsi_check_assign_cmdsn(session,
  313. (struct iscsi_nopin*)hdr);
  314. if (rc)
  315. goto done;
  316. switch(opcode) {
  317. case ISCSI_OP_LOGOUT_RSP:
  318. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  319. /* fall through */
  320. case ISCSI_OP_LOGIN_RSP:
  321. case ISCSI_OP_TEXT_RSP:
  322. /*
  323. * login related PDU's exp_statsn is handled in
  324. * userspace
  325. */
  326. rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
  327. list_del(&mtask->running);
  328. if (conn->login_mtask != mtask)
  329. __kfifo_put(session->mgmtpool.queue,
  330. (void*)&mtask, sizeof(void*));
  331. break;
  332. case ISCSI_OP_SCSI_TMFUNC_RSP:
  333. if (datalen) {
  334. rc = ISCSI_ERR_PROTO;
  335. break;
  336. }
  337. iscsi_tmf_rsp(conn, hdr);
  338. break;
  339. case ISCSI_OP_NOOP_IN:
  340. if (hdr->ttt != ISCSI_RESERVED_TAG) {
  341. rc = ISCSI_ERR_PROTO;
  342. break;
  343. }
  344. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  345. rc = iscsi_recv_pdu(conn->cls_conn, hdr, data, datalen);
  346. list_del(&mtask->running);
  347. if (conn->login_mtask != mtask)
  348. __kfifo_put(session->mgmtpool.queue,
  349. (void*)&mtask, sizeof(void*));
  350. break;
  351. default:
  352. rc = ISCSI_ERR_BAD_OPCODE;
  353. break;
  354. }
  355. } else if (itt == ISCSI_RESERVED_TAG) {
  356. switch(opcode) {
  357. case ISCSI_OP_NOOP_IN:
  358. if (!datalen) {
  359. rc = iscsi_check_assign_cmdsn(session,
  360. (struct iscsi_nopin*)hdr);
  361. if (!rc && hdr->ttt != ISCSI_RESERVED_TAG)
  362. rc = iscsi_recv_pdu(conn->cls_conn,
  363. hdr, NULL, 0);
  364. } else
  365. rc = ISCSI_ERR_PROTO;
  366. break;
  367. case ISCSI_OP_REJECT:
  368. /* we need sth like iscsi_reject_rsp()*/
  369. case ISCSI_OP_ASYNC_EVENT:
  370. conn->exp_statsn = be32_to_cpu(hdr->statsn) + 1;
  371. /* we need sth like iscsi_async_event_rsp() */
  372. rc = ISCSI_ERR_BAD_OPCODE;
  373. break;
  374. default:
  375. rc = ISCSI_ERR_BAD_OPCODE;
  376. break;
  377. }
  378. } else
  379. rc = ISCSI_ERR_BAD_ITT;
  380. done:
  381. return rc;
  382. }
  383. EXPORT_SYMBOL_GPL(__iscsi_complete_pdu);
  384. int iscsi_complete_pdu(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  385. char *data, int datalen)
  386. {
  387. int rc;
  388. spin_lock(&conn->session->lock);
  389. rc = __iscsi_complete_pdu(conn, hdr, data, datalen);
  390. spin_unlock(&conn->session->lock);
  391. return rc;
  392. }
  393. EXPORT_SYMBOL_GPL(iscsi_complete_pdu);
  394. /* verify itt (itt encoding: age+cid+itt) */
  395. int iscsi_verify_itt(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  396. uint32_t *ret_itt)
  397. {
  398. struct iscsi_session *session = conn->session;
  399. struct iscsi_cmd_task *ctask;
  400. uint32_t itt;
  401. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  402. if ((hdr->itt & ISCSI_AGE_MASK) !=
  403. (session->age << ISCSI_AGE_SHIFT)) {
  404. printk(KERN_ERR "iscsi: received itt %x expected "
  405. "session age (%x)\n", hdr->itt,
  406. session->age & ISCSI_AGE_MASK);
  407. return ISCSI_ERR_BAD_ITT;
  408. }
  409. if ((hdr->itt & ISCSI_CID_MASK) !=
  410. (conn->id << ISCSI_CID_SHIFT)) {
  411. printk(KERN_ERR "iscsi: received itt %x, expected "
  412. "CID (%x)\n", hdr->itt, conn->id);
  413. return ISCSI_ERR_BAD_ITT;
  414. }
  415. itt = hdr->itt & ISCSI_ITT_MASK;
  416. } else
  417. itt = hdr->itt;
  418. if (itt < session->cmds_max) {
  419. ctask = session->cmds[itt];
  420. if (!ctask->sc) {
  421. printk(KERN_INFO "iscsi: dropping ctask with "
  422. "itt 0x%x\n", ctask->itt);
  423. /* force drop */
  424. return ISCSI_ERR_NO_SCSI_CMD;
  425. }
  426. if (ctask->sc->SCp.phase != session->age) {
  427. printk(KERN_ERR "iscsi: ctask's session age %d, "
  428. "expected %d\n", ctask->sc->SCp.phase,
  429. session->age);
  430. return ISCSI_ERR_SESSION_FAILED;
  431. }
  432. }
  433. *ret_itt = itt;
  434. return 0;
  435. }
  436. EXPORT_SYMBOL_GPL(iscsi_verify_itt);
  437. void iscsi_conn_failure(struct iscsi_conn *conn, enum iscsi_err err)
  438. {
  439. struct iscsi_session *session = conn->session;
  440. unsigned long flags;
  441. spin_lock_irqsave(&session->lock, flags);
  442. if (session->state == ISCSI_STATE_FAILED) {
  443. spin_unlock_irqrestore(&session->lock, flags);
  444. return;
  445. }
  446. if (conn->stop_stage == 0)
  447. session->state = ISCSI_STATE_FAILED;
  448. spin_unlock_irqrestore(&session->lock, flags);
  449. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  450. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_rx);
  451. iscsi_conn_error(conn->cls_conn, err);
  452. }
  453. EXPORT_SYMBOL_GPL(iscsi_conn_failure);
  454. /**
  455. * iscsi_data_xmit - xmit any command into the scheduled connection
  456. * @conn: iscsi connection
  457. *
  458. * Notes:
  459. * The function can return -EAGAIN in which case the caller must
  460. * re-schedule it again later or recover. '0' return code means
  461. * successful xmit.
  462. **/
  463. static int iscsi_data_xmit(struct iscsi_conn *conn)
  464. {
  465. struct iscsi_transport *tt;
  466. int rc = 0;
  467. if (unlikely(conn->suspend_tx)) {
  468. debug_scsi("conn %d Tx suspended!\n", conn->id);
  469. return -ENODATA;
  470. }
  471. tt = conn->session->tt;
  472. /*
  473. * Transmit in the following order:
  474. *
  475. * 1) un-finished xmit (ctask or mtask)
  476. * 2) immediate control PDUs
  477. * 3) write data
  478. * 4) SCSI commands
  479. * 5) non-immediate control PDUs
  480. *
  481. * No need to lock around __kfifo_get as long as
  482. * there's one producer and one consumer.
  483. */
  484. BUG_ON(conn->ctask && conn->mtask);
  485. if (conn->ctask) {
  486. rc = tt->xmit_cmd_task(conn, conn->ctask);
  487. if (rc)
  488. goto again;
  489. /* done with this in-progress ctask */
  490. conn->ctask = NULL;
  491. }
  492. if (conn->mtask) {
  493. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  494. if (rc)
  495. goto again;
  496. /* done with this in-progress mtask */
  497. conn->mtask = NULL;
  498. }
  499. /* process immediate first */
  500. if (unlikely(__kfifo_len(conn->immqueue))) {
  501. while (__kfifo_get(conn->immqueue, (void*)&conn->mtask,
  502. sizeof(void*))) {
  503. spin_lock_bh(&conn->session->lock);
  504. list_add_tail(&conn->mtask->running,
  505. &conn->mgmt_run_list);
  506. spin_unlock_bh(&conn->session->lock);
  507. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  508. if (rc)
  509. goto again;
  510. }
  511. /* done with this mtask */
  512. conn->mtask = NULL;
  513. }
  514. /* process command queue */
  515. spin_lock_bh(&conn->session->lock);
  516. while (!list_empty(&conn->xmitqueue)) {
  517. /*
  518. * iscsi tcp may readd the task to the xmitqueue to send
  519. * write data
  520. */
  521. conn->ctask = list_entry(conn->xmitqueue.next,
  522. struct iscsi_cmd_task, running);
  523. conn->ctask->state = ISCSI_TASK_RUNNING;
  524. list_move_tail(conn->xmitqueue.next, &conn->run_list);
  525. spin_unlock_bh(&conn->session->lock);
  526. rc = tt->xmit_cmd_task(conn, conn->ctask);
  527. if (rc)
  528. goto again;
  529. spin_lock_bh(&conn->session->lock);
  530. }
  531. spin_unlock_bh(&conn->session->lock);
  532. /* done with this ctask */
  533. conn->ctask = NULL;
  534. /* process the rest control plane PDUs, if any */
  535. if (unlikely(__kfifo_len(conn->mgmtqueue))) {
  536. while (__kfifo_get(conn->mgmtqueue, (void*)&conn->mtask,
  537. sizeof(void*))) {
  538. spin_lock_bh(&conn->session->lock);
  539. list_add_tail(&conn->mtask->running,
  540. &conn->mgmt_run_list);
  541. spin_unlock_bh(&conn->session->lock);
  542. rc = tt->xmit_mgmt_task(conn, conn->mtask);
  543. if (rc)
  544. goto again;
  545. }
  546. /* done with this mtask */
  547. conn->mtask = NULL;
  548. }
  549. return -ENODATA;
  550. again:
  551. if (unlikely(conn->suspend_tx))
  552. return -ENODATA;
  553. return rc;
  554. }
  555. static void iscsi_xmitworker(void *data)
  556. {
  557. struct iscsi_conn *conn = data;
  558. int rc;
  559. /*
  560. * serialize Xmit worker on a per-connection basis.
  561. */
  562. mutex_lock(&conn->xmitmutex);
  563. do {
  564. rc = iscsi_data_xmit(conn);
  565. } while (rc >= 0 || rc == -EAGAIN);
  566. mutex_unlock(&conn->xmitmutex);
  567. }
  568. enum {
  569. FAILURE_BAD_HOST = 1,
  570. FAILURE_SESSION_FAILED,
  571. FAILURE_SESSION_FREED,
  572. FAILURE_WINDOW_CLOSED,
  573. FAILURE_SESSION_TERMINATE,
  574. FAILURE_SESSION_IN_RECOVERY,
  575. FAILURE_SESSION_RECOVERY_TIMEOUT,
  576. };
  577. int iscsi_queuecommand(struct scsi_cmnd *sc, void (*done)(struct scsi_cmnd *))
  578. {
  579. struct Scsi_Host *host;
  580. int reason = 0;
  581. struct iscsi_session *session;
  582. struct iscsi_conn *conn;
  583. struct iscsi_cmd_task *ctask = NULL;
  584. sc->scsi_done = done;
  585. sc->result = 0;
  586. host = sc->device->host;
  587. session = iscsi_hostdata(host->hostdata);
  588. spin_lock(&session->lock);
  589. /*
  590. * ISCSI_STATE_FAILED is a temp. state. The recovery
  591. * code will decide what is best to do with command queued
  592. * during this time
  593. */
  594. if (session->state != ISCSI_STATE_LOGGED_IN &&
  595. session->state != ISCSI_STATE_FAILED) {
  596. /*
  597. * to handle the race between when we set the recovery state
  598. * and block the session we requeue here (commands could
  599. * be entering our queuecommand while a block is starting
  600. * up because the block code is not locked)
  601. */
  602. if (session->state == ISCSI_STATE_IN_RECOVERY) {
  603. reason = FAILURE_SESSION_IN_RECOVERY;
  604. goto reject;
  605. }
  606. if (session->state == ISCSI_STATE_RECOVERY_FAILED)
  607. reason = FAILURE_SESSION_RECOVERY_TIMEOUT;
  608. else if (session->state == ISCSI_STATE_TERMINATE)
  609. reason = FAILURE_SESSION_TERMINATE;
  610. else
  611. reason = FAILURE_SESSION_FREED;
  612. goto fault;
  613. }
  614. /*
  615. * Check for iSCSI window and take care of CmdSN wrap-around
  616. */
  617. if ((int)(session->max_cmdsn - session->cmdsn) < 0) {
  618. reason = FAILURE_WINDOW_CLOSED;
  619. goto reject;
  620. }
  621. conn = session->leadconn;
  622. __kfifo_get(session->cmdpool.queue, (void*)&ctask, sizeof(void*));
  623. sc->SCp.phase = session->age;
  624. sc->SCp.ptr = (char *)ctask;
  625. ctask->state = ISCSI_TASK_PENDING;
  626. ctask->mtask = NULL;
  627. ctask->conn = conn;
  628. ctask->sc = sc;
  629. INIT_LIST_HEAD(&ctask->running);
  630. ctask->total_length = sc->request_bufflen;
  631. iscsi_prep_scsi_cmd_pdu(ctask);
  632. session->tt->init_cmd_task(ctask);
  633. list_add_tail(&ctask->running, &conn->xmitqueue);
  634. debug_scsi(
  635. "ctask enq [%s cid %d sc %lx itt 0x%x len %d cmdsn %d win %d]\n",
  636. sc->sc_data_direction == DMA_TO_DEVICE ? "write" : "read",
  637. conn->id, (long)sc, ctask->itt, sc->request_bufflen,
  638. session->cmdsn, session->max_cmdsn - session->exp_cmdsn + 1);
  639. spin_unlock(&session->lock);
  640. scsi_queue_work(host, &conn->xmitwork);
  641. return 0;
  642. reject:
  643. spin_unlock(&session->lock);
  644. debug_scsi("cmd 0x%x rejected (%d)\n", sc->cmnd[0], reason);
  645. return SCSI_MLQUEUE_HOST_BUSY;
  646. fault:
  647. spin_unlock(&session->lock);
  648. printk(KERN_ERR "iscsi: cmd 0x%x is not queued (%d)\n",
  649. sc->cmnd[0], reason);
  650. sc->result = (DID_NO_CONNECT << 16);
  651. sc->resid = sc->request_bufflen;
  652. sc->scsi_done(sc);
  653. return 0;
  654. }
  655. EXPORT_SYMBOL_GPL(iscsi_queuecommand);
  656. int iscsi_change_queue_depth(struct scsi_device *sdev, int depth)
  657. {
  658. if (depth > ISCSI_MAX_CMD_PER_LUN)
  659. depth = ISCSI_MAX_CMD_PER_LUN;
  660. scsi_adjust_queue_depth(sdev, scsi_get_tag_type(sdev), depth);
  661. return sdev->queue_depth;
  662. }
  663. EXPORT_SYMBOL_GPL(iscsi_change_queue_depth);
  664. static int
  665. iscsi_conn_send_generic(struct iscsi_conn *conn, struct iscsi_hdr *hdr,
  666. char *data, uint32_t data_size)
  667. {
  668. struct iscsi_session *session = conn->session;
  669. struct iscsi_nopout *nop = (struct iscsi_nopout *)hdr;
  670. struct iscsi_mgmt_task *mtask;
  671. spin_lock_bh(&session->lock);
  672. if (session->state == ISCSI_STATE_TERMINATE) {
  673. spin_unlock_bh(&session->lock);
  674. return -EPERM;
  675. }
  676. if (hdr->opcode == (ISCSI_OP_LOGIN | ISCSI_OP_IMMEDIATE) ||
  677. hdr->opcode == (ISCSI_OP_TEXT | ISCSI_OP_IMMEDIATE))
  678. /*
  679. * Login and Text are sent serially, in
  680. * request-followed-by-response sequence.
  681. * Same mtask can be used. Same ITT must be used.
  682. * Note that login_mtask is preallocated at conn_create().
  683. */
  684. mtask = conn->login_mtask;
  685. else {
  686. BUG_ON(conn->c_stage == ISCSI_CONN_INITIAL_STAGE);
  687. BUG_ON(conn->c_stage == ISCSI_CONN_STOPPED);
  688. nop->exp_statsn = cpu_to_be32(conn->exp_statsn);
  689. if (!__kfifo_get(session->mgmtpool.queue,
  690. (void*)&mtask, sizeof(void*))) {
  691. spin_unlock_bh(&session->lock);
  692. return -ENOSPC;
  693. }
  694. }
  695. /*
  696. * pre-format CmdSN for outgoing PDU.
  697. */
  698. if (hdr->itt != cpu_to_be32(ISCSI_RESERVED_TAG)) {
  699. hdr->itt = mtask->itt | (conn->id << ISCSI_CID_SHIFT) |
  700. (session->age << ISCSI_AGE_SHIFT);
  701. nop->cmdsn = cpu_to_be32(session->cmdsn);
  702. if (conn->c_stage == ISCSI_CONN_STARTED &&
  703. !(hdr->opcode & ISCSI_OP_IMMEDIATE))
  704. session->cmdsn++;
  705. } else
  706. /* do not advance CmdSN */
  707. nop->cmdsn = cpu_to_be32(session->cmdsn);
  708. if (data_size) {
  709. memcpy(mtask->data, data, data_size);
  710. mtask->data_count = data_size;
  711. } else
  712. mtask->data_count = 0;
  713. INIT_LIST_HEAD(&mtask->running);
  714. memcpy(mtask->hdr, hdr, sizeof(struct iscsi_hdr));
  715. if (session->tt->init_mgmt_task)
  716. session->tt->init_mgmt_task(conn, mtask, data, data_size);
  717. spin_unlock_bh(&session->lock);
  718. debug_scsi("mgmtpdu [op 0x%x hdr->itt 0x%x datalen %d]\n",
  719. hdr->opcode, hdr->itt, data_size);
  720. /*
  721. * since send_pdu() could be called at least from two contexts,
  722. * we need to serialize __kfifo_put, so we don't have to take
  723. * additional lock on fast data-path
  724. */
  725. if (hdr->opcode & ISCSI_OP_IMMEDIATE)
  726. __kfifo_put(conn->immqueue, (void*)&mtask, sizeof(void*));
  727. else
  728. __kfifo_put(conn->mgmtqueue, (void*)&mtask, sizeof(void*));
  729. scsi_queue_work(session->host, &conn->xmitwork);
  730. return 0;
  731. }
  732. int iscsi_conn_send_pdu(struct iscsi_cls_conn *cls_conn, struct iscsi_hdr *hdr,
  733. char *data, uint32_t data_size)
  734. {
  735. struct iscsi_conn *conn = cls_conn->dd_data;
  736. int rc;
  737. mutex_lock(&conn->xmitmutex);
  738. rc = iscsi_conn_send_generic(conn, hdr, data, data_size);
  739. mutex_unlock(&conn->xmitmutex);
  740. return rc;
  741. }
  742. EXPORT_SYMBOL_GPL(iscsi_conn_send_pdu);
  743. void iscsi_session_recovery_timedout(struct iscsi_cls_session *cls_session)
  744. {
  745. struct iscsi_session *session = class_to_transport_session(cls_session);
  746. struct iscsi_conn *conn = session->leadconn;
  747. spin_lock_bh(&session->lock);
  748. if (session->state != ISCSI_STATE_LOGGED_IN) {
  749. session->state = ISCSI_STATE_RECOVERY_FAILED;
  750. if (conn)
  751. wake_up(&conn->ehwait);
  752. }
  753. spin_unlock_bh(&session->lock);
  754. }
  755. EXPORT_SYMBOL_GPL(iscsi_session_recovery_timedout);
  756. int iscsi_eh_host_reset(struct scsi_cmnd *sc)
  757. {
  758. struct Scsi_Host *host = sc->device->host;
  759. struct iscsi_session *session = iscsi_hostdata(host->hostdata);
  760. struct iscsi_conn *conn = session->leadconn;
  761. int fail_session = 0;
  762. spin_lock_bh(&session->lock);
  763. if (session->state == ISCSI_STATE_TERMINATE) {
  764. failed:
  765. debug_scsi("failing host reset: session terminated "
  766. "[CID %d age %d]", conn->id, session->age);
  767. spin_unlock_bh(&session->lock);
  768. return FAILED;
  769. }
  770. if (sc->SCp.phase == session->age) {
  771. debug_scsi("failing connection CID %d due to SCSI host reset",
  772. conn->id);
  773. fail_session = 1;
  774. }
  775. spin_unlock_bh(&session->lock);
  776. /*
  777. * we drop the lock here but the leadconn cannot be destoyed while
  778. * we are in the scsi eh
  779. */
  780. if (fail_session)
  781. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  782. debug_scsi("iscsi_eh_host_reset wait for relogin\n");
  783. wait_event_interruptible(conn->ehwait,
  784. session->state == ISCSI_STATE_TERMINATE ||
  785. session->state == ISCSI_STATE_LOGGED_IN ||
  786. session->state == ISCSI_STATE_RECOVERY_FAILED);
  787. if (signal_pending(current))
  788. flush_signals(current);
  789. spin_lock_bh(&session->lock);
  790. if (session->state == ISCSI_STATE_LOGGED_IN)
  791. printk(KERN_INFO "iscsi: host reset succeeded\n");
  792. else
  793. goto failed;
  794. spin_unlock_bh(&session->lock);
  795. return SUCCESS;
  796. }
  797. EXPORT_SYMBOL_GPL(iscsi_eh_host_reset);
  798. static void iscsi_tmabort_timedout(unsigned long data)
  799. {
  800. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)data;
  801. struct iscsi_conn *conn = ctask->conn;
  802. struct iscsi_session *session = conn->session;
  803. spin_lock(&session->lock);
  804. if (conn->tmabort_state == TMABORT_INITIAL) {
  805. conn->tmabort_state = TMABORT_TIMEDOUT;
  806. debug_scsi("tmabort timedout [sc %p itt 0x%x]\n",
  807. ctask->sc, ctask->itt);
  808. /* unblock eh_abort() */
  809. wake_up(&conn->ehwait);
  810. }
  811. spin_unlock(&session->lock);
  812. }
  813. /* must be called with the mutex lock */
  814. static int iscsi_exec_abort_task(struct scsi_cmnd *sc,
  815. struct iscsi_cmd_task *ctask)
  816. {
  817. struct iscsi_conn *conn = ctask->conn;
  818. struct iscsi_session *session = conn->session;
  819. struct iscsi_tm *hdr = &conn->tmhdr;
  820. int rc;
  821. /*
  822. * ctask timed out but session is OK requests must be serialized.
  823. */
  824. memset(hdr, 0, sizeof(struct iscsi_tm));
  825. hdr->opcode = ISCSI_OP_SCSI_TMFUNC | ISCSI_OP_IMMEDIATE;
  826. hdr->flags = ISCSI_TM_FUNC_ABORT_TASK;
  827. hdr->flags |= ISCSI_FLAG_CMD_FINAL;
  828. memcpy(hdr->lun, ctask->hdr->lun, sizeof(hdr->lun));
  829. hdr->rtt = ctask->hdr->itt;
  830. hdr->refcmdsn = ctask->hdr->cmdsn;
  831. rc = iscsi_conn_send_generic(conn, (struct iscsi_hdr *)hdr,
  832. NULL, 0);
  833. if (rc) {
  834. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  835. debug_scsi("abort sent failure [itt 0x%x] %d", ctask->itt, rc);
  836. return rc;
  837. }
  838. debug_scsi("abort sent [itt 0x%x]\n", ctask->itt);
  839. spin_lock_bh(&session->lock);
  840. ctask->mtask = (struct iscsi_mgmt_task *)
  841. session->mgmt_cmds[(hdr->itt & ISCSI_ITT_MASK) -
  842. ISCSI_MGMT_ITT_OFFSET];
  843. if (conn->tmabort_state == TMABORT_INITIAL) {
  844. conn->tmfcmd_pdus_cnt++;
  845. conn->tmabort_timer.expires = 10*HZ + jiffies;
  846. conn->tmabort_timer.function = iscsi_tmabort_timedout;
  847. conn->tmabort_timer.data = (unsigned long)ctask;
  848. add_timer(&conn->tmabort_timer);
  849. debug_scsi("abort set timeout [itt 0x%x]", ctask->itt);
  850. }
  851. spin_unlock_bh(&session->lock);
  852. mutex_unlock(&conn->xmitmutex);
  853. /*
  854. * block eh thread until:
  855. *
  856. * 1) abort response
  857. * 2) abort timeout
  858. * 3) session is terminated or restarted or userspace has
  859. * given up on recovery
  860. */
  861. wait_event_interruptible(conn->ehwait,
  862. sc->SCp.phase != session->age ||
  863. session->state != ISCSI_STATE_LOGGED_IN ||
  864. conn->tmabort_state != TMABORT_INITIAL);
  865. if (signal_pending(current))
  866. flush_signals(current);
  867. del_timer_sync(&conn->tmabort_timer);
  868. mutex_lock(&conn->xmitmutex);
  869. return 0;
  870. }
  871. /*
  872. * xmit mutex and session lock must be held
  873. */
  874. static struct iscsi_mgmt_task *
  875. iscsi_remove_mgmt_task(struct kfifo *fifo, uint32_t itt)
  876. {
  877. int i, nr_tasks = __kfifo_len(fifo) / sizeof(void*);
  878. struct iscsi_mgmt_task *task;
  879. debug_scsi("searching %d tasks\n", nr_tasks);
  880. for (i = 0; i < nr_tasks; i++) {
  881. __kfifo_get(fifo, (void*)&task, sizeof(void*));
  882. debug_scsi("check task %u\n", task->itt);
  883. if (task->itt == itt) {
  884. debug_scsi("matched task\n");
  885. return task;
  886. }
  887. __kfifo_put(fifo, (void*)&task, sizeof(void*));
  888. }
  889. return NULL;
  890. }
  891. static int iscsi_ctask_mtask_cleanup(struct iscsi_cmd_task *ctask)
  892. {
  893. struct iscsi_conn *conn = ctask->conn;
  894. struct iscsi_session *session = conn->session;
  895. if (!ctask->mtask)
  896. return -EINVAL;
  897. if (!iscsi_remove_mgmt_task(conn->immqueue, ctask->mtask->itt))
  898. list_del(&ctask->mtask->running);
  899. __kfifo_put(session->mgmtpool.queue, (void*)&ctask->mtask,
  900. sizeof(void*));
  901. ctask->mtask = NULL;
  902. return 0;
  903. }
  904. /*
  905. * session lock and xmitmutex must be held
  906. */
  907. static void fail_command(struct iscsi_conn *conn, struct iscsi_cmd_task *ctask,
  908. int err)
  909. {
  910. struct scsi_cmnd *sc;
  911. sc = ctask->sc;
  912. if (!sc)
  913. return;
  914. conn->session->tt->cleanup_cmd_task(conn, ctask);
  915. iscsi_ctask_mtask_cleanup(ctask);
  916. sc->result = err;
  917. sc->resid = sc->request_bufflen;
  918. iscsi_complete_command(conn->session, ctask);
  919. }
  920. int iscsi_eh_abort(struct scsi_cmnd *sc)
  921. {
  922. struct iscsi_cmd_task *ctask = (struct iscsi_cmd_task *)sc->SCp.ptr;
  923. struct iscsi_conn *conn = ctask->conn;
  924. struct iscsi_session *session = conn->session;
  925. int rc;
  926. conn->eh_abort_cnt++;
  927. debug_scsi("aborting [sc %p itt 0x%x]\n", sc, ctask->itt);
  928. mutex_lock(&conn->xmitmutex);
  929. spin_lock_bh(&session->lock);
  930. /*
  931. * If we are not logged in or we have started a new session
  932. * then let the host reset code handle this
  933. */
  934. if (session->state != ISCSI_STATE_LOGGED_IN ||
  935. sc->SCp.phase != session->age)
  936. goto failed;
  937. /* ctask completed before time out */
  938. if (!ctask->sc) {
  939. spin_unlock_bh(&session->lock);
  940. debug_scsi("sc completed while abort in progress\n");
  941. goto success_rel_mutex;
  942. }
  943. /* what should we do here ? */
  944. if (conn->ctask == ctask) {
  945. printk(KERN_INFO "iscsi: sc %p itt 0x%x partially sent. "
  946. "Failing abort\n", sc, ctask->itt);
  947. goto failed;
  948. }
  949. if (ctask->state == ISCSI_TASK_PENDING)
  950. goto success_cleanup;
  951. conn->tmabort_state = TMABORT_INITIAL;
  952. spin_unlock_bh(&session->lock);
  953. rc = iscsi_exec_abort_task(sc, ctask);
  954. spin_lock_bh(&session->lock);
  955. if (rc || sc->SCp.phase != session->age ||
  956. session->state != ISCSI_STATE_LOGGED_IN)
  957. goto failed;
  958. iscsi_ctask_mtask_cleanup(ctask);
  959. switch (conn->tmabort_state) {
  960. case TMABORT_SUCCESS:
  961. goto success_cleanup;
  962. case TMABORT_NOT_FOUND:
  963. if (!ctask->sc) {
  964. /* ctask completed before tmf abort response */
  965. spin_unlock_bh(&session->lock);
  966. debug_scsi("sc completed while abort in progress\n");
  967. goto success_rel_mutex;
  968. }
  969. /* fall through */
  970. default:
  971. /* timedout or failed */
  972. spin_unlock_bh(&session->lock);
  973. iscsi_conn_failure(conn, ISCSI_ERR_CONN_FAILED);
  974. spin_lock_bh(&session->lock);
  975. goto failed;
  976. }
  977. success_cleanup:
  978. debug_scsi("abort success [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  979. spin_unlock_bh(&session->lock);
  980. /*
  981. * clean up task if aborted. we have the xmitmutex so grab
  982. * the recv lock as a writer
  983. */
  984. write_lock_bh(conn->recv_lock);
  985. spin_lock(&session->lock);
  986. fail_command(conn, ctask, DID_ABORT << 16);
  987. spin_unlock(&session->lock);
  988. write_unlock_bh(conn->recv_lock);
  989. success_rel_mutex:
  990. mutex_unlock(&conn->xmitmutex);
  991. return SUCCESS;
  992. failed:
  993. spin_unlock_bh(&session->lock);
  994. mutex_unlock(&conn->xmitmutex);
  995. debug_scsi("abort failed [sc %lx itt 0x%x]\n", (long)sc, ctask->itt);
  996. return FAILED;
  997. }
  998. EXPORT_SYMBOL_GPL(iscsi_eh_abort);
  999. int
  1000. iscsi_pool_init(struct iscsi_queue *q, int max, void ***items, int item_size)
  1001. {
  1002. int i;
  1003. *items = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1004. if (*items == NULL)
  1005. return -ENOMEM;
  1006. q->max = max;
  1007. q->pool = kmalloc(max * sizeof(void*), GFP_KERNEL);
  1008. if (q->pool == NULL) {
  1009. kfree(*items);
  1010. return -ENOMEM;
  1011. }
  1012. q->queue = kfifo_init((void*)q->pool, max * sizeof(void*),
  1013. GFP_KERNEL, NULL);
  1014. if (q->queue == ERR_PTR(-ENOMEM)) {
  1015. kfree(q->pool);
  1016. kfree(*items);
  1017. return -ENOMEM;
  1018. }
  1019. for (i = 0; i < max; i++) {
  1020. q->pool[i] = kmalloc(item_size, GFP_KERNEL);
  1021. if (q->pool[i] == NULL) {
  1022. int j;
  1023. for (j = 0; j < i; j++)
  1024. kfree(q->pool[j]);
  1025. kfifo_free(q->queue);
  1026. kfree(q->pool);
  1027. kfree(*items);
  1028. return -ENOMEM;
  1029. }
  1030. memset(q->pool[i], 0, item_size);
  1031. (*items)[i] = q->pool[i];
  1032. __kfifo_put(q->queue, (void*)&q->pool[i], sizeof(void*));
  1033. }
  1034. return 0;
  1035. }
  1036. EXPORT_SYMBOL_GPL(iscsi_pool_init);
  1037. void iscsi_pool_free(struct iscsi_queue *q, void **items)
  1038. {
  1039. int i;
  1040. for (i = 0; i < q->max; i++)
  1041. kfree(items[i]);
  1042. kfree(q->pool);
  1043. kfree(items);
  1044. }
  1045. EXPORT_SYMBOL_GPL(iscsi_pool_free);
  1046. /*
  1047. * iSCSI Session's hostdata organization:
  1048. *
  1049. * *------------------* <== hostdata_session(host->hostdata)
  1050. * | ptr to class sess|
  1051. * |------------------| <== iscsi_hostdata(host->hostdata)
  1052. * | iscsi_session |
  1053. * *------------------*
  1054. */
  1055. #define hostdata_privsize(_sz) (sizeof(unsigned long) + _sz + \
  1056. _sz % sizeof(unsigned long))
  1057. #define hostdata_session(_hostdata) (iscsi_ptr(*(unsigned long *)_hostdata))
  1058. /**
  1059. * iscsi_session_setup - create iscsi cls session and host and session
  1060. * @scsit: scsi transport template
  1061. * @iscsit: iscsi transport template
  1062. * @initial_cmdsn: initial CmdSN
  1063. * @hostno: host no allocated
  1064. *
  1065. * This can be used by software iscsi_transports that allocate
  1066. * a session per scsi host.
  1067. **/
  1068. struct iscsi_cls_session *
  1069. iscsi_session_setup(struct iscsi_transport *iscsit,
  1070. struct scsi_transport_template *scsit,
  1071. int cmd_task_size, int mgmt_task_size,
  1072. uint32_t initial_cmdsn, uint32_t *hostno)
  1073. {
  1074. struct Scsi_Host *shost;
  1075. struct iscsi_session *session;
  1076. struct iscsi_cls_session *cls_session;
  1077. int cmd_i;
  1078. shost = scsi_host_alloc(iscsit->host_template,
  1079. hostdata_privsize(sizeof(*session)));
  1080. if (!shost)
  1081. return NULL;
  1082. shost->max_id = 1;
  1083. shost->max_channel = 0;
  1084. shost->max_lun = iscsit->max_lun;
  1085. shost->max_cmd_len = iscsit->max_cmd_len;
  1086. shost->transportt = scsit;
  1087. shost->transportt->create_work_queue = 1;
  1088. *hostno = shost->host_no;
  1089. session = iscsi_hostdata(shost->hostdata);
  1090. memset(session, 0, sizeof(struct iscsi_session));
  1091. session->host = shost;
  1092. session->state = ISCSI_STATE_FREE;
  1093. session->mgmtpool_max = ISCSI_MGMT_CMDS_MAX;
  1094. session->cmds_max = ISCSI_XMIT_CMDS_MAX;
  1095. session->cmdsn = initial_cmdsn;
  1096. session->exp_cmdsn = initial_cmdsn + 1;
  1097. session->max_cmdsn = initial_cmdsn + 1;
  1098. session->max_r2t = 1;
  1099. session->tt = iscsit;
  1100. /* initialize SCSI PDU commands pool */
  1101. if (iscsi_pool_init(&session->cmdpool, session->cmds_max,
  1102. (void***)&session->cmds,
  1103. cmd_task_size + sizeof(struct iscsi_cmd_task)))
  1104. goto cmdpool_alloc_fail;
  1105. /* pre-format cmds pool with ITT */
  1106. for (cmd_i = 0; cmd_i < session->cmds_max; cmd_i++) {
  1107. struct iscsi_cmd_task *ctask = session->cmds[cmd_i];
  1108. if (cmd_task_size)
  1109. ctask->dd_data = &ctask[1];
  1110. ctask->itt = cmd_i;
  1111. INIT_LIST_HEAD(&ctask->running);
  1112. }
  1113. spin_lock_init(&session->lock);
  1114. INIT_LIST_HEAD(&session->connections);
  1115. /* initialize immediate command pool */
  1116. if (iscsi_pool_init(&session->mgmtpool, session->mgmtpool_max,
  1117. (void***)&session->mgmt_cmds,
  1118. mgmt_task_size + sizeof(struct iscsi_mgmt_task)))
  1119. goto mgmtpool_alloc_fail;
  1120. /* pre-format immediate cmds pool with ITT */
  1121. for (cmd_i = 0; cmd_i < session->mgmtpool_max; cmd_i++) {
  1122. struct iscsi_mgmt_task *mtask = session->mgmt_cmds[cmd_i];
  1123. if (mgmt_task_size)
  1124. mtask->dd_data = &mtask[1];
  1125. mtask->itt = ISCSI_MGMT_ITT_OFFSET + cmd_i;
  1126. INIT_LIST_HEAD(&mtask->running);
  1127. }
  1128. if (scsi_add_host(shost, NULL))
  1129. goto add_host_fail;
  1130. if (!try_module_get(iscsit->owner))
  1131. goto cls_session_fail;
  1132. cls_session = iscsi_create_session(shost, iscsit, 0);
  1133. if (!cls_session)
  1134. goto module_put;
  1135. *(unsigned long*)shost->hostdata = (unsigned long)cls_session;
  1136. return cls_session;
  1137. module_put:
  1138. module_put(iscsit->owner);
  1139. cls_session_fail:
  1140. scsi_remove_host(shost);
  1141. add_host_fail:
  1142. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1143. mgmtpool_alloc_fail:
  1144. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1145. cmdpool_alloc_fail:
  1146. scsi_host_put(shost);
  1147. return NULL;
  1148. }
  1149. EXPORT_SYMBOL_GPL(iscsi_session_setup);
  1150. /**
  1151. * iscsi_session_teardown - destroy session, host, and cls_session
  1152. * shost: scsi host
  1153. *
  1154. * This can be used by software iscsi_transports that allocate
  1155. * a session per scsi host.
  1156. **/
  1157. void iscsi_session_teardown(struct iscsi_cls_session *cls_session)
  1158. {
  1159. struct Scsi_Host *shost = iscsi_session_to_shost(cls_session);
  1160. struct iscsi_session *session = iscsi_hostdata(shost->hostdata);
  1161. scsi_remove_host(shost);
  1162. iscsi_pool_free(&session->mgmtpool, (void**)session->mgmt_cmds);
  1163. iscsi_pool_free(&session->cmdpool, (void**)session->cmds);
  1164. iscsi_destroy_session(cls_session);
  1165. scsi_host_put(shost);
  1166. module_put(cls_session->transport->owner);
  1167. }
  1168. EXPORT_SYMBOL_GPL(iscsi_session_teardown);
  1169. /**
  1170. * iscsi_conn_setup - create iscsi_cls_conn and iscsi_conn
  1171. * @cls_session: iscsi_cls_session
  1172. * @conn_idx: cid
  1173. **/
  1174. struct iscsi_cls_conn *
  1175. iscsi_conn_setup(struct iscsi_cls_session *cls_session, uint32_t conn_idx)
  1176. {
  1177. struct iscsi_session *session = class_to_transport_session(cls_session);
  1178. struct iscsi_conn *conn;
  1179. struct iscsi_cls_conn *cls_conn;
  1180. char *data;
  1181. cls_conn = iscsi_create_conn(cls_session, conn_idx);
  1182. if (!cls_conn)
  1183. return NULL;
  1184. conn = cls_conn->dd_data;
  1185. memset(conn, 0, sizeof(*conn));
  1186. conn->session = session;
  1187. conn->cls_conn = cls_conn;
  1188. conn->c_stage = ISCSI_CONN_INITIAL_STAGE;
  1189. conn->id = conn_idx;
  1190. conn->exp_statsn = 0;
  1191. conn->tmabort_state = TMABORT_INITIAL;
  1192. INIT_LIST_HEAD(&conn->run_list);
  1193. INIT_LIST_HEAD(&conn->mgmt_run_list);
  1194. INIT_LIST_HEAD(&conn->xmitqueue);
  1195. /* initialize general immediate & non-immediate PDU commands queue */
  1196. conn->immqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1197. GFP_KERNEL, NULL);
  1198. if (conn->immqueue == ERR_PTR(-ENOMEM))
  1199. goto immqueue_alloc_fail;
  1200. conn->mgmtqueue = kfifo_alloc(session->mgmtpool_max * sizeof(void*),
  1201. GFP_KERNEL, NULL);
  1202. if (conn->mgmtqueue == ERR_PTR(-ENOMEM))
  1203. goto mgmtqueue_alloc_fail;
  1204. INIT_WORK(&conn->xmitwork, iscsi_xmitworker, conn);
  1205. /* allocate login_mtask used for the login/text sequences */
  1206. spin_lock_bh(&session->lock);
  1207. if (!__kfifo_get(session->mgmtpool.queue,
  1208. (void*)&conn->login_mtask,
  1209. sizeof(void*))) {
  1210. spin_unlock_bh(&session->lock);
  1211. goto login_mtask_alloc_fail;
  1212. }
  1213. spin_unlock_bh(&session->lock);
  1214. data = kmalloc(DEFAULT_MAX_RECV_DATA_SEGMENT_LENGTH, GFP_KERNEL);
  1215. if (!data)
  1216. goto login_mtask_data_alloc_fail;
  1217. conn->login_mtask->data = data;
  1218. init_timer(&conn->tmabort_timer);
  1219. mutex_init(&conn->xmitmutex);
  1220. init_waitqueue_head(&conn->ehwait);
  1221. return cls_conn;
  1222. login_mtask_data_alloc_fail:
  1223. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1224. sizeof(void*));
  1225. login_mtask_alloc_fail:
  1226. kfifo_free(conn->mgmtqueue);
  1227. mgmtqueue_alloc_fail:
  1228. kfifo_free(conn->immqueue);
  1229. immqueue_alloc_fail:
  1230. iscsi_destroy_conn(cls_conn);
  1231. return NULL;
  1232. }
  1233. EXPORT_SYMBOL_GPL(iscsi_conn_setup);
  1234. /**
  1235. * iscsi_conn_teardown - teardown iscsi connection
  1236. * cls_conn: iscsi class connection
  1237. *
  1238. * TODO: we may need to make this into a two step process
  1239. * like scsi-mls remove + put host
  1240. */
  1241. void iscsi_conn_teardown(struct iscsi_cls_conn *cls_conn)
  1242. {
  1243. struct iscsi_conn *conn = cls_conn->dd_data;
  1244. struct iscsi_session *session = conn->session;
  1245. unsigned long flags;
  1246. set_bit(ISCSI_SUSPEND_BIT, &conn->suspend_tx);
  1247. mutex_lock(&conn->xmitmutex);
  1248. if (conn->c_stage == ISCSI_CONN_INITIAL_STAGE) {
  1249. if (session->tt->suspend_conn_recv)
  1250. session->tt->suspend_conn_recv(conn);
  1251. session->tt->terminate_conn(conn);
  1252. }
  1253. spin_lock_bh(&session->lock);
  1254. conn->c_stage = ISCSI_CONN_CLEANUP_WAIT;
  1255. if (session->leadconn == conn) {
  1256. /*
  1257. * leading connection? then give up on recovery.
  1258. */
  1259. session->state = ISCSI_STATE_TERMINATE;
  1260. wake_up(&conn->ehwait);
  1261. }
  1262. spin_unlock_bh(&session->lock);
  1263. mutex_unlock(&conn->xmitmutex);
  1264. /*
  1265. * Block until all in-progress commands for this connection
  1266. * time out or fail.
  1267. */
  1268. for (;;) {
  1269. spin_lock_irqsave(session->host->host_lock, flags);
  1270. if (!session->host->host_busy) { /* OK for ERL == 0 */
  1271. spin_unlock_irqrestore(session->host->host_lock, flags);
  1272. break;
  1273. }
  1274. spin_unlock_irqrestore(session->host->host_lock, flags);
  1275. msleep_interruptible(500);
  1276. printk(KERN_INFO "iscsi: scsi conn_destroy(): host_busy %d "
  1277. "host_failed %d\n", session->host->host_busy,
  1278. session->host->host_failed);
  1279. /*
  1280. * force eh_abort() to unblock
  1281. */
  1282. wake_up(&conn->ehwait);
  1283. }
  1284. spin_lock_bh(&session->lock);
  1285. kfree(conn->login_mtask->data);
  1286. __kfifo_put(session->mgmtpool.queue, (void*)&conn->login_mtask,
  1287. sizeof(void*));
  1288. list_del(&conn->item);
  1289. if (list_empty(&session->connections))
  1290. session->leadconn = NULL;
  1291. if (session->leadconn && session->leadconn == conn)
  1292. session->leadconn = container_of(session->connections.next,
  1293. struct iscsi_conn, item);
  1294. if (session->leadconn == NULL)
  1295. /* no connections exits.. reset sequencing */
  1296. session->cmdsn = session->max_cmdsn = session->exp_cmdsn = 1;
  1297. spin_unlock_bh(&session->lock);
  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. list_for_each_entry_safe(ctask, tmp, &conn->xmitqueue, running) {
  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");