libiscsi.c 52 KB

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