zfcp_scsi.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  1. /*
  2. * zfcp device driver
  3. *
  4. * Interface to Linux SCSI midlayer.
  5. *
  6. * Copyright IBM Corporation 2002, 2008
  7. */
  8. #include "zfcp_ext.h"
  9. #include <asm/atomic.h>
  10. /* Find start of Sense Information in FCP response unit*/
  11. char *zfcp_get_fcp_sns_info_ptr(struct fcp_rsp_iu *fcp_rsp_iu)
  12. {
  13. char *fcp_sns_info_ptr;
  14. fcp_sns_info_ptr = (unsigned char *) &fcp_rsp_iu[1];
  15. if (fcp_rsp_iu->validity.bits.fcp_rsp_len_valid)
  16. fcp_sns_info_ptr += fcp_rsp_iu->fcp_rsp_len;
  17. return fcp_sns_info_ptr;
  18. }
  19. void zfcp_set_fcp_dl(struct fcp_cmnd_iu *fcp_cmd, fcp_dl_t fcp_dl)
  20. {
  21. fcp_dl_t *fcp_dl_ptr;
  22. /*
  23. * fcp_dl_addr = start address of fcp_cmnd structure +
  24. * size of fixed part + size of dynamically sized add_dcp_cdb field
  25. * SEE FCP-2 documentation
  26. */
  27. fcp_dl_ptr = (fcp_dl_t *) ((unsigned char *) &fcp_cmd[1] +
  28. (fcp_cmd->add_fcp_cdb_length << 2));
  29. *fcp_dl_ptr = fcp_dl;
  30. }
  31. static void zfcp_scsi_slave_destroy(struct scsi_device *sdpnt)
  32. {
  33. struct zfcp_unit *unit = (struct zfcp_unit *) sdpnt->hostdata;
  34. WARN_ON(!unit);
  35. if (unit) {
  36. atomic_clear_mask(ZFCP_STATUS_UNIT_REGISTERED, &unit->status);
  37. sdpnt->hostdata = NULL;
  38. unit->device = NULL;
  39. zfcp_erp_unit_failed(unit, 12, NULL);
  40. zfcp_unit_put(unit);
  41. }
  42. }
  43. static int zfcp_scsi_slave_configure(struct scsi_device *sdp)
  44. {
  45. if (sdp->tagged_supported)
  46. scsi_adjust_queue_depth(sdp, MSG_SIMPLE_TAG, 32);
  47. else
  48. scsi_adjust_queue_depth(sdp, 0, 1);
  49. return 0;
  50. }
  51. static void zfcp_scsi_command_fail(struct scsi_cmnd *scpnt, int result)
  52. {
  53. set_host_byte(scpnt, result);
  54. if ((scpnt->device != NULL) && (scpnt->device->host != NULL))
  55. zfcp_scsi_dbf_event_result("fail", 4,
  56. (struct zfcp_adapter*) scpnt->device->host->hostdata[0],
  57. scpnt, NULL);
  58. /* return directly */
  59. scpnt->scsi_done(scpnt);
  60. }
  61. static int zfcp_scsi_queuecommand(struct scsi_cmnd *scpnt,
  62. void (*done) (struct scsi_cmnd *))
  63. {
  64. struct zfcp_unit *unit;
  65. struct zfcp_adapter *adapter;
  66. int status;
  67. int ret;
  68. /* reset the status for this request */
  69. scpnt->result = 0;
  70. scpnt->host_scribble = NULL;
  71. scpnt->scsi_done = done;
  72. /*
  73. * figure out adapter and target device
  74. * (stored there by zfcp_scsi_slave_alloc)
  75. */
  76. adapter = (struct zfcp_adapter *) scpnt->device->host->hostdata[0];
  77. unit = scpnt->device->hostdata;
  78. BUG_ON(!adapter || (adapter != unit->port->adapter));
  79. BUG_ON(!scpnt->scsi_done);
  80. if (unlikely(!unit)) {
  81. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  82. return 0;
  83. }
  84. status = atomic_read(&unit->status);
  85. if (unlikely((status & ZFCP_STATUS_COMMON_ERP_FAILED) ||
  86. !(status & ZFCP_STATUS_COMMON_RUNNING))) {
  87. zfcp_scsi_command_fail(scpnt, DID_ERROR);
  88. return 0;;
  89. }
  90. ret = zfcp_fsf_send_fcp_command_task(adapter, unit, scpnt, 0,
  91. ZFCP_REQ_AUTO_CLEANUP);
  92. if (unlikely(ret == -EBUSY))
  93. zfcp_scsi_command_fail(scpnt, DID_NO_CONNECT);
  94. else if (unlikely(ret < 0))
  95. return SCSI_MLQUEUE_HOST_BUSY;
  96. return ret;
  97. }
  98. static struct zfcp_unit *zfcp_unit_lookup(struct zfcp_adapter *adapter,
  99. int channel, unsigned int id,
  100. unsigned int lun)
  101. {
  102. struct zfcp_port *port;
  103. struct zfcp_unit *unit;
  104. list_for_each_entry(port, &adapter->port_list_head, list) {
  105. if (!port->rport || (id != port->rport->scsi_target_id))
  106. continue;
  107. list_for_each_entry(unit, &port->unit_list_head, list)
  108. if (lun == unit->scsi_lun)
  109. return unit;
  110. }
  111. return NULL;
  112. }
  113. static int zfcp_scsi_slave_alloc(struct scsi_device *sdp)
  114. {
  115. struct zfcp_adapter *adapter;
  116. struct zfcp_unit *unit;
  117. unsigned long flags;
  118. int retval = -ENXIO;
  119. adapter = (struct zfcp_adapter *) sdp->host->hostdata[0];
  120. if (!adapter)
  121. goto out;
  122. read_lock_irqsave(&zfcp_data.config_lock, flags);
  123. unit = zfcp_unit_lookup(adapter, sdp->channel, sdp->id, sdp->lun);
  124. if (unit &&
  125. (atomic_read(&unit->status) & ZFCP_STATUS_UNIT_REGISTERED)) {
  126. sdp->hostdata = unit;
  127. unit->device = sdp;
  128. zfcp_unit_get(unit);
  129. retval = 0;
  130. }
  131. read_unlock_irqrestore(&zfcp_data.config_lock, flags);
  132. out:
  133. return retval;
  134. }
  135. static int zfcp_scsi_eh_abort_handler(struct scsi_cmnd *scpnt)
  136. {
  137. struct Scsi_Host *scsi_host;
  138. struct zfcp_adapter *adapter;
  139. struct zfcp_unit *unit;
  140. struct zfcp_fsf_req *fsf_req;
  141. unsigned long flags;
  142. unsigned long old_req_id = (unsigned long) scpnt->host_scribble;
  143. int retval = SUCCESS;
  144. scsi_host = scpnt->device->host;
  145. adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
  146. unit = scpnt->device->hostdata;
  147. /* avoid race condition between late normal completion and abort */
  148. write_lock_irqsave(&adapter->abort_lock, flags);
  149. /* Check whether corresponding fsf_req is still pending */
  150. spin_lock(&adapter->req_list_lock);
  151. fsf_req = zfcp_reqlist_find(adapter, old_req_id);
  152. spin_unlock(&adapter->req_list_lock);
  153. if (!fsf_req) {
  154. write_unlock_irqrestore(&adapter->abort_lock, flags);
  155. zfcp_scsi_dbf_event_abort("lte1", adapter, scpnt, NULL, 0);
  156. return retval;
  157. }
  158. fsf_req->data = NULL;
  159. fsf_req->status |= ZFCP_STATUS_FSFREQ_ABORTING;
  160. /* don't access old fsf_req after releasing the abort_lock */
  161. write_unlock_irqrestore(&adapter->abort_lock, flags);
  162. fsf_req = zfcp_fsf_abort_fcp_command(old_req_id, adapter, unit, 0);
  163. if (!fsf_req) {
  164. zfcp_scsi_dbf_event_abort("nres", adapter, scpnt, NULL,
  165. old_req_id);
  166. retval = FAILED;
  167. return retval;
  168. }
  169. __wait_event(fsf_req->completion_wq,
  170. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  171. if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTSUCCEEDED) {
  172. zfcp_scsi_dbf_event_abort("okay", adapter, scpnt, fsf_req, 0);
  173. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_ABORTNOTNEEDED) {
  174. zfcp_scsi_dbf_event_abort("lte2", adapter, scpnt, fsf_req, 0);
  175. } else {
  176. zfcp_scsi_dbf_event_abort("fail", adapter, scpnt, fsf_req, 0);
  177. retval = FAILED;
  178. }
  179. zfcp_fsf_req_free(fsf_req);
  180. return retval;
  181. }
  182. static int zfcp_task_mgmt_function(struct zfcp_unit *unit, u8 tm_flags,
  183. struct scsi_cmnd *scpnt)
  184. {
  185. struct zfcp_adapter *adapter = unit->port->adapter;
  186. struct zfcp_fsf_req *fsf_req;
  187. int retval = SUCCESS;
  188. /* issue task management function */
  189. fsf_req = zfcp_fsf_send_fcp_ctm(adapter, unit, tm_flags, 0);
  190. if (!fsf_req) {
  191. zfcp_scsi_dbf_event_devreset("nres", tm_flags, unit, scpnt);
  192. return FAILED;
  193. }
  194. __wait_event(fsf_req->completion_wq,
  195. fsf_req->status & ZFCP_STATUS_FSFREQ_COMPLETED);
  196. /*
  197. * check completion status of task management function
  198. */
  199. if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCFAILED) {
  200. zfcp_scsi_dbf_event_devreset("fail", tm_flags, unit, scpnt);
  201. retval = FAILED;
  202. } else if (fsf_req->status & ZFCP_STATUS_FSFREQ_TMFUNCNOTSUPP) {
  203. zfcp_scsi_dbf_event_devreset("nsup", tm_flags, unit, scpnt);
  204. retval = FAILED;
  205. } else
  206. zfcp_scsi_dbf_event_devreset("okay", tm_flags, unit, scpnt);
  207. zfcp_fsf_req_free(fsf_req);
  208. return retval;
  209. }
  210. static int zfcp_scsi_eh_device_reset_handler(struct scsi_cmnd *scpnt)
  211. {
  212. struct zfcp_unit *unit = scpnt->device->hostdata;
  213. if (!unit) {
  214. WARN_ON(1);
  215. return SUCCESS;
  216. }
  217. return zfcp_task_mgmt_function(unit, FCP_LOGICAL_UNIT_RESET, scpnt);
  218. }
  219. static int zfcp_scsi_eh_target_reset_handler(struct scsi_cmnd *scpnt)
  220. {
  221. struct zfcp_unit *unit = scpnt->device->hostdata;
  222. if (!unit) {
  223. WARN_ON(1);
  224. return SUCCESS;
  225. }
  226. return zfcp_task_mgmt_function(unit, FCP_TARGET_RESET, scpnt);
  227. }
  228. static int zfcp_scsi_eh_host_reset_handler(struct scsi_cmnd *scpnt)
  229. {
  230. struct zfcp_unit *unit;
  231. struct zfcp_adapter *adapter;
  232. unit = scpnt->device->hostdata;
  233. adapter = unit->port->adapter;
  234. zfcp_erp_adapter_reopen(adapter, 0, 141, scpnt);
  235. zfcp_erp_wait(adapter);
  236. return SUCCESS;
  237. }
  238. int zfcp_adapter_scsi_register(struct zfcp_adapter *adapter)
  239. {
  240. struct ccw_dev_id dev_id;
  241. if (adapter->scsi_host)
  242. return 0;
  243. ccw_device_get_id(adapter->ccw_device, &dev_id);
  244. /* register adapter as SCSI host with mid layer of SCSI stack */
  245. adapter->scsi_host = scsi_host_alloc(&zfcp_data.scsi_host_template,
  246. sizeof (struct zfcp_adapter *));
  247. if (!adapter->scsi_host) {
  248. dev_err(&adapter->ccw_device->dev,
  249. "registration with SCSI stack failed.");
  250. return -EIO;
  251. }
  252. /* tell the SCSI stack some characteristics of this adapter */
  253. adapter->scsi_host->max_id = 1;
  254. adapter->scsi_host->max_lun = 1;
  255. adapter->scsi_host->max_channel = 0;
  256. adapter->scsi_host->unique_id = dev_id.devno;
  257. adapter->scsi_host->max_cmd_len = 255;
  258. adapter->scsi_host->transportt = zfcp_data.scsi_transport_template;
  259. adapter->scsi_host->hostdata[0] = (unsigned long) adapter;
  260. if (scsi_add_host(adapter->scsi_host, &adapter->ccw_device->dev)) {
  261. scsi_host_put(adapter->scsi_host);
  262. return -EIO;
  263. }
  264. atomic_set_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  265. return 0;
  266. }
  267. void zfcp_adapter_scsi_unregister(struct zfcp_adapter *adapter)
  268. {
  269. struct Scsi_Host *shost;
  270. struct zfcp_port *port;
  271. shost = adapter->scsi_host;
  272. if (!shost)
  273. return;
  274. read_lock_irq(&zfcp_data.config_lock);
  275. list_for_each_entry(port, &adapter->port_list_head, list)
  276. if (port->rport)
  277. port->rport = NULL;
  278. read_unlock_irq(&zfcp_data.config_lock);
  279. fc_remove_host(shost);
  280. scsi_remove_host(shost);
  281. scsi_host_put(shost);
  282. adapter->scsi_host = NULL;
  283. atomic_clear_mask(ZFCP_STATUS_ADAPTER_REGISTERED, &adapter->status);
  284. return;
  285. }
  286. static struct fc_host_statistics*
  287. zfcp_init_fc_host_stats(struct zfcp_adapter *adapter)
  288. {
  289. struct fc_host_statistics *fc_stats;
  290. if (!adapter->fc_stats) {
  291. fc_stats = kmalloc(sizeof(*fc_stats), GFP_KERNEL);
  292. if (!fc_stats)
  293. return NULL;
  294. adapter->fc_stats = fc_stats; /* freed in adater_dequeue */
  295. }
  296. memset(adapter->fc_stats, 0, sizeof(*adapter->fc_stats));
  297. return adapter->fc_stats;
  298. }
  299. static void zfcp_adjust_fc_host_stats(struct fc_host_statistics *fc_stats,
  300. struct fsf_qtcb_bottom_port *data,
  301. struct fsf_qtcb_bottom_port *old)
  302. {
  303. fc_stats->seconds_since_last_reset =
  304. data->seconds_since_last_reset - old->seconds_since_last_reset;
  305. fc_stats->tx_frames = data->tx_frames - old->tx_frames;
  306. fc_stats->tx_words = data->tx_words - old->tx_words;
  307. fc_stats->rx_frames = data->rx_frames - old->rx_frames;
  308. fc_stats->rx_words = data->rx_words - old->rx_words;
  309. fc_stats->lip_count = data->lip - old->lip;
  310. fc_stats->nos_count = data->nos - old->nos;
  311. fc_stats->error_frames = data->error_frames - old->error_frames;
  312. fc_stats->dumped_frames = data->dumped_frames - old->dumped_frames;
  313. fc_stats->link_failure_count = data->link_failure - old->link_failure;
  314. fc_stats->loss_of_sync_count = data->loss_of_sync - old->loss_of_sync;
  315. fc_stats->loss_of_signal_count =
  316. data->loss_of_signal - old->loss_of_signal;
  317. fc_stats->prim_seq_protocol_err_count =
  318. data->psp_error_counts - old->psp_error_counts;
  319. fc_stats->invalid_tx_word_count =
  320. data->invalid_tx_words - old->invalid_tx_words;
  321. fc_stats->invalid_crc_count = data->invalid_crcs - old->invalid_crcs;
  322. fc_stats->fcp_input_requests =
  323. data->input_requests - old->input_requests;
  324. fc_stats->fcp_output_requests =
  325. data->output_requests - old->output_requests;
  326. fc_stats->fcp_control_requests =
  327. data->control_requests - old->control_requests;
  328. fc_stats->fcp_input_megabytes = data->input_mb - old->input_mb;
  329. fc_stats->fcp_output_megabytes = data->output_mb - old->output_mb;
  330. }
  331. static void zfcp_set_fc_host_stats(struct fc_host_statistics *fc_stats,
  332. struct fsf_qtcb_bottom_port *data)
  333. {
  334. fc_stats->seconds_since_last_reset = data->seconds_since_last_reset;
  335. fc_stats->tx_frames = data->tx_frames;
  336. fc_stats->tx_words = data->tx_words;
  337. fc_stats->rx_frames = data->rx_frames;
  338. fc_stats->rx_words = data->rx_words;
  339. fc_stats->lip_count = data->lip;
  340. fc_stats->nos_count = data->nos;
  341. fc_stats->error_frames = data->error_frames;
  342. fc_stats->dumped_frames = data->dumped_frames;
  343. fc_stats->link_failure_count = data->link_failure;
  344. fc_stats->loss_of_sync_count = data->loss_of_sync;
  345. fc_stats->loss_of_signal_count = data->loss_of_signal;
  346. fc_stats->prim_seq_protocol_err_count = data->psp_error_counts;
  347. fc_stats->invalid_tx_word_count = data->invalid_tx_words;
  348. fc_stats->invalid_crc_count = data->invalid_crcs;
  349. fc_stats->fcp_input_requests = data->input_requests;
  350. fc_stats->fcp_output_requests = data->output_requests;
  351. fc_stats->fcp_control_requests = data->control_requests;
  352. fc_stats->fcp_input_megabytes = data->input_mb;
  353. fc_stats->fcp_output_megabytes = data->output_mb;
  354. }
  355. static struct fc_host_statistics *zfcp_get_fc_host_stats(struct Scsi_Host *host)
  356. {
  357. struct zfcp_adapter *adapter;
  358. struct fc_host_statistics *fc_stats;
  359. struct fsf_qtcb_bottom_port *data;
  360. int ret;
  361. adapter = (struct zfcp_adapter *)host->hostdata[0];
  362. fc_stats = zfcp_init_fc_host_stats(adapter);
  363. if (!fc_stats)
  364. return NULL;
  365. data = kzalloc(sizeof(*data), GFP_KERNEL);
  366. if (!data)
  367. return NULL;
  368. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  369. if (ret) {
  370. kfree(data);
  371. return NULL;
  372. }
  373. if (adapter->stats_reset &&
  374. ((jiffies/HZ - adapter->stats_reset) <
  375. data->seconds_since_last_reset))
  376. zfcp_adjust_fc_host_stats(fc_stats, data,
  377. adapter->stats_reset_data);
  378. else
  379. zfcp_set_fc_host_stats(fc_stats, data);
  380. kfree(data);
  381. return fc_stats;
  382. }
  383. static void zfcp_reset_fc_host_stats(struct Scsi_Host *shost)
  384. {
  385. struct zfcp_adapter *adapter;
  386. struct fsf_qtcb_bottom_port *data;
  387. int ret;
  388. adapter = (struct zfcp_adapter *)shost->hostdata[0];
  389. data = kzalloc(sizeof(*data), GFP_KERNEL);
  390. if (!data)
  391. return;
  392. ret = zfcp_fsf_exchange_port_data_sync(adapter, data);
  393. if (ret)
  394. kfree(data);
  395. else {
  396. adapter->stats_reset = jiffies/HZ;
  397. kfree(adapter->stats_reset_data);
  398. adapter->stats_reset_data = data; /* finally freed in
  399. adapter_dequeue */
  400. }
  401. }
  402. static void zfcp_get_host_port_state(struct Scsi_Host *shost)
  403. {
  404. struct zfcp_adapter *adapter =
  405. (struct zfcp_adapter *)shost->hostdata[0];
  406. int status = atomic_read(&adapter->status);
  407. if ((status & ZFCP_STATUS_COMMON_RUNNING) &&
  408. !(status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED))
  409. fc_host_port_state(shost) = FC_PORTSTATE_ONLINE;
  410. else if (status & ZFCP_STATUS_ADAPTER_LINK_UNPLUGGED)
  411. fc_host_port_state(shost) = FC_PORTSTATE_LINKDOWN;
  412. else if (status & ZFCP_STATUS_COMMON_ERP_FAILED)
  413. fc_host_port_state(shost) = FC_PORTSTATE_ERROR;
  414. else
  415. fc_host_port_state(shost) = FC_PORTSTATE_UNKNOWN;
  416. }
  417. static void zfcp_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  418. {
  419. rport->dev_loss_tmo = timeout;
  420. }
  421. struct fc_function_template zfcp_transport_functions = {
  422. .show_starget_port_id = 1,
  423. .show_starget_port_name = 1,
  424. .show_starget_node_name = 1,
  425. .show_rport_supported_classes = 1,
  426. .show_rport_maxframe_size = 1,
  427. .show_rport_dev_loss_tmo = 1,
  428. .show_host_node_name = 1,
  429. .show_host_port_name = 1,
  430. .show_host_permanent_port_name = 1,
  431. .show_host_supported_classes = 1,
  432. .show_host_supported_speeds = 1,
  433. .show_host_maxframe_size = 1,
  434. .show_host_serial_number = 1,
  435. .get_fc_host_stats = zfcp_get_fc_host_stats,
  436. .reset_fc_host_stats = zfcp_reset_fc_host_stats,
  437. .set_rport_dev_loss_tmo = zfcp_set_rport_dev_loss_tmo,
  438. .get_host_port_state = zfcp_get_host_port_state,
  439. .show_host_port_state = 1,
  440. /* no functions registered for following dynamic attributes but
  441. directly set by LLDD */
  442. .show_host_port_type = 1,
  443. .show_host_speed = 1,
  444. .show_host_port_id = 1,
  445. .disable_target_scan = 1,
  446. };
  447. struct zfcp_data zfcp_data = {
  448. .scsi_host_template = {
  449. .name = "zfcp",
  450. .module = THIS_MODULE,
  451. .proc_name = "zfcp",
  452. .slave_alloc = zfcp_scsi_slave_alloc,
  453. .slave_configure = zfcp_scsi_slave_configure,
  454. .slave_destroy = zfcp_scsi_slave_destroy,
  455. .queuecommand = zfcp_scsi_queuecommand,
  456. .eh_abort_handler = zfcp_scsi_eh_abort_handler,
  457. .eh_device_reset_handler = zfcp_scsi_eh_device_reset_handler,
  458. .eh_target_reset_handler = zfcp_scsi_eh_target_reset_handler,
  459. .eh_host_reset_handler = zfcp_scsi_eh_host_reset_handler,
  460. .can_queue = 4096,
  461. .this_id = -1,
  462. .sg_tablesize = ZFCP_MAX_SBALES_PER_REQ,
  463. .cmd_per_lun = 1,
  464. .use_clustering = 1,
  465. .sdev_attrs = zfcp_sysfs_sdev_attrs,
  466. .max_sectors = (ZFCP_MAX_SBALES_PER_REQ * 8),
  467. .shost_attrs = zfcp_sysfs_shost_attrs,
  468. },
  469. };